C# 删除带有正斜杠的url ASP.NET Webforms,IIS7
我正在维护现有的ASP.NET Webforms应用程序。出现了一个问题,我们现在需要保证从所有URL中删除尾部斜杠C# 删除带有正斜杠的url ASP.NET Webforms,IIS7,c#,webforms,iis-7,C#,Webforms,Iis 7,我正在维护现有的ASP.NET Webforms应用程序。出现了一个问题,我们现在需要保证从所有URL中删除尾部斜杠 https://www.example.com/blah.aspx --> https://www.example.com/blah.aspx https://www.example.com/blah --> https://www.example.com/blah https://www.example.com/blah/ --> https://www
https://www.example.com/blah.aspx --> https://www.example.com/blah.aspx
https://www.example.com/blah --> https://www.example.com/blah
https://www.example.com/blah/ --> https://www.example.com/blah
https://www.example.com/blah/?a=1 --> https://www.example.com/blah?a=1
现在,这是一个已经存在了很多年的应用程序,URL是在代码隐藏中建立的,直接在aspx文件中建立的——基本上无处不在。添加到混合中的是,尽管是ASPX应用程序,但它配置为使用System.Web.routing
进行路由。因此,我们也可能有:
定义中不带尾随斜杠
aRoutes.MapPageRoute("routeBrandsCamp",
"brand/{name}/camp",
"~/Pages/Brand/Camp.aspx", true, new RouteValueDictionary
{
{"name", " "}
});
aRoutes.MapPageRoute("routeBrandsSummary",
"brand/{name}/summary/",
"~/Pages/Brand/Summary.aspx", true, new RouteValueDictionary
{
{"name", " "}
});
因此,解决方案似乎是我需要在IIS中添加重写规则,或者我应该在中执行一些操作,比如说BeginRequest()
无论是什么解决方案,您还可以提供一个工作示例吗?例如IIS重写规则,一些代码等
基本上,我想在一个地方更改它,而不是破坏应用程序;-) IIS重写模块在
system.webServer
部分下保留一个节点,允许我们非常轻松地在那里配置设置
在system.webServer
节点下添加以下代码:
<rewrite>
<rules>
<!--To always remove trailing slash from the URL-->
<rule name="Remove trailing slash" stopProcessing="true">
<match url="(.*)/$" />
<conditions>
<add input="{REQUEST_FILENAME}" matchType="IsFile" negate="true" />
<add input="{REQUEST_FILENAME}" matchType="IsDirectory" negate="true" />
</conditions>
<action type="Redirect" redirectType="Permanent" url="{R:1}" />
</rule>
</rules>
</rewrite>
,是否要从URL末尾删除正斜杠?是。。。。你看到我作为例子提供的url列表了吗?