简体   繁体   English

从请求中删除default.aspx

[英]remove default.aspx from a request

i am trying to remove default.aspx from any request that might have it. 我试图从任何可能有它的请求中删除default.aspx。

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 日Thnx

k got it. k得到了它。

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 :) 和你们说的一起:)

我认为,如果你把重定向放在里面,如果你不必处理无限重定向。

You are endlessly redirecting. 你无休止地重定向。

Each time the following line executes the Application_BeginRequest event is fired again. 每次执行以下行时,都会再次触发Application_BeginRequest事件。

context.Response.Redirect(url);

Put the redirect inside the if statement like this. 将重定向放在if语句中,如下所示。

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM