2016-08-02 2 views
1

こんにちは、私はプログラムでwebconfigのページリダイレクトを変更したいと思います。 私はWeb設定で次のコードを持っています。Web設定のページリダイレクトをプログラムで変更する

<location path="WebForm2.aspx"> 
    <system.webServer> 
     <httpRedirect enabled="true" destination="http://google.com" httpResponseStatus="Permanent" /> 
    </system.webServer> 
    </location> 

私はプログラムでc#を使用してhttpredirectを有効または無効にしたいと考えています。 どうすればいいか教えてください。

+0

このhttp: /stackoverflow.com/questions/270287/editing-web-config-programatically – Rahul

答えて

0

私はRahulによって提案されたコードを試しましたが、Web.config<location>要素をプログラム的に変更することができませんでした。

ハンドラ::ウェブで

namespace StackOverflowHelp 
{ 
    public class RedirectHandler : IHttpHandler 
    { 
     public bool IsReusable 
     { 
      get { return true; } 
     } 

     public void ProcessRequest(HttpContext context) 
     { 
      Uri uri = context.Request.Url; 

      if (uri.AbsolutePath == "/WebForm2.aspx.aspx") 
       context.Response.Redirect("http://google.com"); 
     } 
    } 
} 

ハンドラの登録代替あなたはWebForm2.aspxページへのリクエストをインターセプトし、別のURLにユーザーをリダイレクトするためのHttpHandlerを書くことができますよう

。 config:

<system.webServer> 
    <handlers> 
     <add verb="*" path="WebForm2.aspx" 
     name="RedirectHandler" 
     type="StackOverflowHelp.RedirectHandler"/> 
    </handlers> 
+0

私は実行時にwebconfigののパスを変更するか、実行時にhttpRedirectの有効化または無効化を変更します。私はコードのページをリダイレクトしたくありません.Webconfigからしか望みません。私はページソースにリダイレクションコードを入れないでください。 webcofigを変更するには、私がリダイレクトする同じページではない別のページを使用します。 –

関連する問題