简体   繁体   中英

root directory of MVC not found

I'm new to MVC4 (or MVC at all using .NET). 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?

Your default route is handled by "RouteConfig.cs" which is in the folder "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. I prefer using attributeRouting. It allows to define the routes via an attribute above your controller. http://attributerouting.net/ or more precisely 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 . Index() then you would do

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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