ASP.NET MVC 3 Controller route - make everything under home controller appear under the domain

前端 未结 4 1233
你的背包
你的背包 2020-12-14 18:55

Currently everything under homecontroller appears in the URL as

example.com/Home/{Action}

Is there a way we can keep all other routing the

4条回答
  •  眼角桃花
    2020-12-14 19:39

    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).

提交回复
热议问题