ASP.NET MVC 3 Controller route - make everything u

2019-01-22 20:19发布

问题:

Currently everything under homecontroller appears in the URL as

example.com/Home/{Action}

Is there a way we can keep all other routing the way it is but ONLY special case home controller so everything under home comes under the domain.

like

example.com/about
example.com/contact
example.com/error

instead of creating new controller classes for each of them.

EDIT:

The other URL's like

example.com/user/details/123
example.com/user/edit/123

Which are in the userController should work the same as they are now

回答1:

I think the best way is:

 routes.MapRoute("home", "home", new { controller = "Home", action = "Index" });
 routes.MapRoute("about", "about", new { controller = "Home", action = "About" });
 routes.MapRoute("contact", "contact", new { controller = "Home", action = "Contact" });

 routes.MapRoute(
     "Default", // Route name
     "{controller}/{action}/{id}", // URL with parameters
     new { controller = "Home", action = "Index", id = UrlParameter.Optional } // Parameter defaults
 );

and when you want to create a link, use:

@Html.RouteLink("Home", "home", new{/* route values */}, new {/* html attribues */})

OR:

@Html.RouteLink("Home", "home")

instead of:

@Html.ActionLink("Home", "Index", "Home", new{/* route values */}, new {/* html attribues */})

this works for me, and should work for you too.

UPDATE:

you can create a symbol like @ (or - or anything else), before the action part in url, to make the url unique, such as:

routes.MapRoute(
    "test",  // route name
    "@{action}", // url and parameters
    new {controller = "MyHome", action = "Home"} // parameter defaults
    );
routes.MapRoute(
    "Default", // Route name
    "{controller}/{action}/{id}", // URL with parameters
    new { controller = "Home", action = "Index", id = UrlParameter.Optional } // Parameter defaults
     );

in this way, your urls are different from the Default map-route and you can create urls like:

site.com/@Home
site.com/@About
site.com/@Contact

but the first, in my idea, is better and I always use that.



回答2:

Using the Attribute Routing of MVC5, I did similar to Javad_Amiry answer, by placing a route for each action in HomeController:

public class HomeController : Controller
{
    [Route("about")]
    public ActionResult About()
    {
        return View();
    }

I think this is more maintainable than placing every action in the global RouteConfig.cs file. Better still to combine Attribute Routing with convention-based routing, so new actions added to controller will work by default without a Route attribute (eg: /Home/action) but can be improved by adding the Route attribute (eg: /action).



回答3:

You could simply modify the default route and remove the controller bit from the url and specify that it will always be Home in the default values:

routes.MapRoute(
    "Default",
    "{action}/{id}",
    new { controller = "Home", action = "Index", id = UrlParameter.Optional }
);

Obviously you realize that this limits your application to a single controller which is HomeController as now you no longer have any possibility to set it in your url. Stuffing all the actions in a single controller is a bad practice IMHO and violates a couple of principles like RESTful routing and SRP.



回答4:

ASP.NET MVC root url’s with generic routing