1

Hi i want to change the Page redirection in webconfig programmatically. i have following code in web config.

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

i want to enable or disable the httpredirect programmatically using c#. please suggest me how to do this.

Jeetendra negi
  • 187
  • 2
  • 9
  • have you checked this http://stackoverflow.com/questions/270287/editing-web-config-programatically – Rahul Aug 02 '16 at 18:04

1 Answers1

0

I've tried the code suggested by Rahul but I wasn't able to change the <location> element of the Web.config programatically.

As an alternative you could write an HttpHandler to intercept the request to the WebForm2.aspx page and redirect the user to another URL:

Handler:

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");
        }
    }
}

Handler registration in the Web.config:

  <system.webServer>
    <handlers>
      <add verb="*" path="WebForm2.aspx"
       name="RedirectHandler"
       type="StackOverflowHelp.RedirectHandler"/>
    </handlers>
Denys Wessels
  • 16,829
  • 14
  • 80
  • 120
  • Sir i want to change the path in in webconfig at runtime or change the httpRedirect enabling or disabling at runtime. i dont want to redirect the page from code behind i want it from webconfig only. i dont put any redirection code in the page source. For changing the webcofig i use another page not the same page whose i redirect to. – Jeetendra negi Aug 04 '16 at 15:46