在Asp Mvc网站上显示默认路由的完整URL

时间:2014-09-12 16:32:20

标签: asp.net-mvc-4 iis

当有人导航到我网站的根目录时,我想显示完整的网址。如果他们导航到www.mysite.com,默认路由会正确处理它并显示正确的页面。问题是浏览器中的URL显示www.mysite.com而不是www.mysite.com/unity/hygiene(设置为默认路由)。

我尝试在global.asax

中添加一个开始请求
void Application_BeginRequest(Object sender, EventArgs e)
{
    var ctx = HttpContext.Current;
    var originalPath = ctx.Request.RawUrl.ToLower();
    if (originalPath == "/") 
        ctx.RewritePath("/unity/hygiene/");
}   

它会重写路径,但不会更新URL。

我在IIS(IIS 7.5,Server 2008R2)中安装了URL Rewrite,但我也没有成功。我尝试了一个rewriteMap:

<rewrite>
    <rewriteMaps>
        <rewriteMap name="HygieneDefault">
            <add key="http://www.example.com:8002/" value="http://www.example.com:8002/unity/hygiene/" />
        </rewriteMap>
    </rewriteMaps>
    <globalRules>
    </globalRules>
</rewrite>

如何在浏览器中显示默认路由的完整URL?

1 个答案:

答案 0 :(得分:1)

我能找到的唯一解决方案是在控制器中检查当前URL,如果是根则重定向。

var url = Request.RawUrl;
if (url == @"/")
{
    Response.Redirect("/Unity/Hygiene");
}
return View();