Asp.Net路由-显示完整的URL [英] Asp.Net Routing - Display complete 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,包括控制器和Action?
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?
推荐答案
一种选择是将您的默认路由设置为一个新的控制器,可能通过操作Root
将其命名为BaseController
:
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屋!