我是MVC4(或使用.NET的MVC)的新手。我的控制器和视图工作正常,但如果我导航到
http://localhost:<port>
我得到了404.如果我去“
http://localhost:<port>/MyController
一切正常。如何获取网站ROOT的默认控制器?
答案 0 :(得分:2)
您的默认路由由位于“App_Start”文件夹中的“RouteConfig.cs”处理
routes.MapRoute(
name: "Default",
url: "{controller}/{action}/{id}",
defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional }
事实上,我再也不会使用这个RouteConfig了。我更喜欢使用attributeRouting。它允许通过控制器上方的属性定义路径。 http://attributerouting.net/或更准确地说http://attributerouting.net/#defining-routes
答案 1 :(得分:1)
这取决于你的&#39; root控制器&#39;被调用以及着陆操作的名称是什么。如果是MyController
。Index()
那么你会做
routes.MapRoute(
name: "Default",
url: "{controller}/{action}/{id}",
defaults: new { controller = "My", action = "Index", id = UrlParameter.Optional }