在Global.asax中重写网址 [英] Url Rewriting in Global.asax

查看:85
本文介绍了在Global.asax中重写网址的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

嗨朋友们,

我想重写我的网址,所以我编写了以下代码。

Hi friends,
I want to rewrite my URL,so i have written the following code.

protected void Application_BeginRequest(object sender, EventArgs e)
        {
   		HttpContext context = HttpContext.Current;
                string path = context.Request.Path.ToLower();
                if(path.Equals("/AndhraPradesh.aspx"))
                {
                    context.RewritePath("~/AP");
                }
        }





但我仍然得到网址为http:// localhost:22313 / AndhraPradesh .aspx



请帮帮我,建议我在global.asax重写网址



感谢你



But still i am getting url as "http://localhost:22313/AndhraPradesh.aspx"

Please Help me and suggest me about url rewriting in global.asax

Thanking you

推荐答案

string path = context.Request.Path.ToLower();
                if(path.Equals("/AndhraPradesh.aspx"))





路径是小写的,你可以用带有大写字母的字符串进行比较。



path is in lowercase and you compare to a string with uppercases.


看看这些链接是否有用:



使用ASP.NET重写URL [ ^ ]



URL重写ASP .NET [ ^ ]



此外,短网址不包含任何扩展名,因此IIS无论如何都无法处理它并会抛出404,以管理无扩展名的网址需要在服务器si上安装第三方ISAPI过滤器德。但是,如果您使用的是IIS7,则可以使用名为URLRewrite的内置工具来处理(尽管不确定)。另外,我建议使用RegEx进行路径模式匹配,这是大多数URL重写引擎的工作方式。



或者你可以尝试使用path.contains,而不是path.Equals正如此链接所示:



RewritePath [ ^ ]



希望这有助于...干杯。
See if these links help:

URL Rewriting with ASP.NET[^]

URL rewriting ASP.NET[^]

Also, the short URL doesn't contain any extensions so the IIS wouldn't be able to process it anyway and would throw a 404, to manage extensionless URLs you would need a third party ISAPI filter installed on the server side. However, if you are using IIS7 then an inbuilt tool called URLRewrite may be able to take care of that (not sure though). Plus, i would recommend using RegEx for path pattern matching which is how most URL rewriting engines work.

Alternatively you can try using path.contains , instead of path.Equals as this link suggests:

RewritePath[^]

Hope this helps...Cheers.


Try this.

protected void Application_BeginRequest(object sender, EventArgs e)
        {
   		HttpContext context = HttpContext.Current;
                string path = context.Request.Path.ToLower();
                if(path.Equals("/andhrapradesh.aspx"))
                {
                    context.RewritePath("~/AP");
                }
        }


这篇关于在Global.asax中重写网址的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆