MVC 5 Routing Attribute

后端 未结 4 1319
梦如初夏
梦如初夏 2020-12-19 00:37

I have the Home Controller and my Action name is Index. In My route config the routes like below.

routes.MapRoute(
    \"Default\",   // Route name
    \"{c         


        
4条回答
  •  别那么骄傲
    2020-12-19 01:02

    For Attribute Routing in ASP.NET MVC 5

    decorate your controller like this

    [RoutePrefix("Home")]
    public HomeController : Controller {
        //GET Home/Index
        [HttpGet]
        [Route("Index")]
        public ActionResult Index() {
            return View(); 
        }
    }
    

    And enable it in route table like this

    public class RouteConfig {
    
        public static void RegisterRoutes(RouteCollection routes) {
            routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
    
            //enable attribute routing
            routes.MapMvcAttributeRoutes();
    
            //convention-based routes
            routes.MapRoute(
                name: "Default",
                url: "{controller}/{action}/{id}",
                defaults: new { controller = "Home", action = "Index", id = "" }
            );
        }
    }
    

提交回复
热议问题