URL重写 - 添加默认页域

问题描述:

我要定义控制器和动作添加到URL基本URL重写 - 添加默认页域

localhost:6473 -> localhost:6473/Beta/Index 

与URL在Web.config中重写,但由于某种原因,这是行不通的

<rewrite> 
    <rules> 
    <rule name="Beta_Local" stopProcessing="true"> 
     <match url="(localhost*)" ignoreCase="true" /> 
     <conditions> 
     <add input="{HTTP_HOST}" pattern= "^localhost:[0-9]{4}$" negate="true"/> 
     </conditions> 
     <action type="Redirect" url="{R:0}/Beta/Index" redirectType="Permanent" /> 
    </rule> 
    </rules> 
</rewrite> 

匹配网址不应包含域名,但路径。如果你想捕捉根/你需要

<match url="^$" /> 

http://www.iis.net/learn/extensions/url-rewrite-module/url-rewrite-module-configuration-reference

而且,看来你并不需要附加条件的本地主机。

完整的规则可能是以下

<rewrite> 
    <rules> 
    <rule name="Beta_Local" stopProcessing="true"> 
     <match url="^$" /> 
     <action type="Redirect" url="/Beta/Index" redirectType="Permanent" /> 
    </rule> 
    </rules> 
</rewrite> 

附:

如果您需要做的是在MVC的方式,你可以使用路由

routes.MapRoute(
    name: "Default", 
    url: "{controller}/{action}/{id}", 
    defaults: new { controller = "Beta", action = "Index", id = UrlParameter.Optional } 
); 
+0

感谢的。它确实有效。做路由已经太晚了,因为路由引擎添加Beta和Index对于最终用户来说是不可见的,但我希望确切的URL域/ Beta/Index作为默认值始终可见(对于谷歌分析) – Andriy