Asp.Net 路由 - 显示完整的 URL [英] Asp.Net Routing - Display complete URL

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

问题描述

我有一个域http://www.abc.com".我已经在这个域上部署了一个 ASP.net MVC4 应用程序.我也在RouteConfig.cs中配置了一个默认路由,如下图

I have a domain "http://www.abc.com". I have deployed an ASP.net MVC4 app on this domain. I have also configured a default route in RouteConfig.cs as shown below

routes.MapRoute(
                name: "Default",
                url: "{controller}/{action}/{id}",
                defaults: new { controller = "MyApp", action = "Home", id = UrlParameter.Optional }
            );

上述映射确保任何尝试访问http://www.abc.com"的人都会自动显示http://www.abc.com/MyApp/Home"的页面

The above mapping ensures that anyone attempting to visit "http://www.abc.com" is automatically shown the page for "http://www.abc.com/MyApp/Home"

一切正常,但浏览器中的地址栏显示http://www.abc.com"而不是http://www.abc.com/MyApp/Home".有没有办法强制浏览器显示完整的 URL,包括控制器和操作?

Everything works as expected but the address bar in the browser shows "http://www.abc.com" instead of "http://www.abc.com/MyApp/Home". Is there any way to force the browser to show the complete URL including the controller and Action?

推荐答案

一种选择是将您的默认路由设置为一个新的控制器,可能称为 BaseController 并带有 Root:

One option would be to set your default route to a new controller, maybe called BaseController with an action Root:

public class BaseController : Controller
{
    public ActionResult Root()
    {
        return RedirectToAction("Home","MyApp");
    }
}

并修改您的 RouteConfig 以指向根请求:

and modify your RouteConfig to point to that for root requests:

routes.MapRoute(
    name: "Default",
    url: "{controller}/{action}/{id}",
    defaults: new { controller = "Base", action = "Root", id = UrlParameter.Optional }
);

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

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