How to map a route for /News/5 to my news controller

后端 未结 2 849
耶瑟儿~
耶瑟儿~ 2021-02-13 15:46

I am trying to identify how to map a route for /News/5 to my news controller.

This is my NewsController:

public class NewsController : BaseController
{
          


        
2条回答
  •  温柔的废话
    2021-02-13 15:53

    You need to make sure your new route is before your default route, like so:

        routes.MapRoute(
            "NewsAbbr", // Route name
            "{controller}/{id}", // URL with parameters
            new { controller = "News", action = "Index", id = -1 } // Parameter defaults
        );
    
    
        routes.MapRoute(
            "News", // Route name
            "{controller}/{action}/{id}", // URL with parameters
            new { controller = "News", action = "Index", id = -1 } // Parameter defaults
        );
    

提交回复
热议问题