简体   繁体   English

找不到MVC的根目录

[英]root directory of MVC not found

I'm new to MVC4 (or MVC at all using .NET). 我是MVC4(或使用.NET的MVC)的新手。 I have my controller and views working fine but if I navigate to http://localhost:<port> I get a 404. If I go to " http://localhost:<port>/MyController everything works fine. How do I get a default controller for ROOT of the website? 我有我的控制器和视图工作正常但如果我导航到http://localhost:<port>我得到404.如果我去“ http://localhost:<port>/MyController一切正常。我怎么办获取网站ROOT的默认控制器?

Your default route is handled by "RouteConfig.cs" which is in the folder "App_Start" 您的默认路由由“RouteConfig.cs”处理,该文件位于“App_Start”文件夹中

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

In fact, I never use this RouteConfig anymore. 事实上,我再也不会使用这个RouteConfig了。 I prefer using attributeRouting. 我更喜欢使用attributeRouting。 It allows to define the routes via an attribute above your controller. 它允许通过控制器上方的属性定义路径。 http://attributerouting.net/ or more precisely http://attributerouting.net/#defining-routes http://attributerouting.net/或者更确切地说是http://attributerouting.net/#defining-routes

It depends on what your 'root controller' is called and what the name of your landing action is. 这取决于您的“根控制器”被调用的内容以及着陆操作的名称。 if it is MyController . 如果它是MyController Index() then you would do 然后你会做的Index()

routes.MapRoute(
        name: "Default",
        url: "{controller}/{action}/{id}",
        defaults: new { controller = "My", action = "Index", id = UrlParameter.Optional }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM