在web.config中重写URL c#
问题描述:
我想重写一个url以匹配页面文件名的确切名称。 我的意思是:我有一个http://example.com/PagePage.aspx,并在浏览器栏上打字http://example.com/pagepage.aspx我想再次获得http://example.com/PagePage.aspx。在web.config中重写URL c#
当然,上键入字符的任意组合,即Pagepage.aspx,pagePage.aspx相同,等等...
我试过这样:
<system.webServer>
<rewrite>
<rules>
<rule name="SpecificRewrite" stopProcessing="true">
<match url="^pagepage$" />
<match url="^Pagepage$" />
<match url="^pagePage$" />
<action type="Rewrite" url="PagePage.aspx" />
</rule>
</rules>
</rewrite>
</system.webServer>
,但我得到“500 - 内部服务器错误”。
所以我也试过这样:
<system.webServer>
<rewrite>
<rules>
<rule name="SpecificRewrite" stopProcessing="true">
<match url="^pagepage\/?$" />
<match url="^Pagepage\/?$" />
<match url="^pagePage\/?$" />
<action type="Redirect" url="/PagePage.aspx" />
</rule>
</rules>
</rewrite>
</system.webServer>
,但我得到一个无限循环
我尝试使用的Global.asax
protected void Application_BeginRequest(object sender, EventArgs e)
{
HttpContext context = HttpContext.Current;
string path = context.Request.Path;
if (path.Equals("/pagepage.aspx"))
{
context.RewritePath(path.Replace("pagepage.aspx", "PagePage.aspx"));
// or context.RewritePath("PagePage.aspx"); is the same
}
}
但要获得同样的,一次。 ..我得到一个无限循环错误...
我GOOGLE了这个问题,但...我发现的方法,更多或更少,我已经尝试过。我错在哪里?
对此有何建议? 谢谢您提前
答
首先,一个规则不能有多个匹配元素。这可能是你得到500错误的原因。其次,我认为你的意思是重定向而不是重写。因此,以下内容将检查结束pagepage.aspx的请求,而不管字母大小写和条件是否将检查URL是否以PagePage.aspx结尾,因此它不会无限循环。这里是:
<rule name="SpecificRewrite" stopProcessing="true">
<match url="^pagepage.aspx$" />
<conditions>
<add input="{REQUEST_URI}" pattern="PagePage.aspx" ignoreCase="false" negate="true" />
</conditions>
<action type="Redirect" url="PagePage.aspx" />
</rule>