在Asp Mvc网站上显示默认路由的完整URL [英] Display full url for default route in Asp Mvc website

查看:107
本文介绍了在Asp Mvc网站上显示默认路由的完整URL的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I would like to display the full URL when someone navigates to the root of my website. If they navigate to www.mysite.com, the default routing properly handles it and displays the correct page. The issue is that the URL in browser displays www.mysite.com instead of www.mysite.com/unity/hygiene (which is set up as the default route).

我尝试在global.asax中添加一个开始请求

I've tried add a begin request in the 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。

It does rewrite the path, but that does not update the URL.

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

I have URL Rewrite installed in IIS (IIS 7.5, Server 2008R2), but I haven't been successful with that either. I tried a 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?

How do I display the full URL in the browser for the default route?

推荐答案

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

The only solution I could find was to check in the controller for the current URL and redirect if it was the root.

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

这篇关于在Asp Mvc网站上显示默认路由的完整URL的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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