views:

108

answers:

3

i am trying to remove default.aspx from any request that might have it.

protected void Application_BeginRequest(object sender, EventArgs e)
        {
            HttpContext context = HttpContext.Current;
            string url = context.Request.Url.ToString();

            // remove default.aspx
            if (url.EndsWith("/default.aspx", StringComparison.OrdinalIgnoreCase))
            {
                url = url.Substring(0, url.Length - 12);
                context.Response.Redirect(url);
            }

        }

gives an error:

**too many redirects occurred trying to open...**

what can i change to make it work?

thnx

+1  A: 

I think that if you put the redirect inside the if you don't have to deal with infinite redirects.

Elph
tried that. same thing happens.
b0x0rz
+1  A: 

You are endlessly redirecting.

Each time the following line executes the Application_BeginRequest event is fired again.

context.Response.Redirect(url);

Put the redirect inside the if statement like this.

if (url.EndsWith("/default.aspx", StringComparison.OrdinalIgnoreCase))
{
    url = url.Substring(0, url.Length - 12);
    context.Response.Redirect(url);
}
Marc Tidd
tried that. same thing happens.
b0x0rz
probably the default.aspx gets appended since it is listed as default page. how to avoid it??
b0x0rz
+2  A: 

k got it.

instead of using:

string url = context.Request.Url.ToString();

i tried:

string url = context.Request.RawUrl.ToString();

and that WORKS! together with what you guys said :)

b0x0rz