Asp.Net MVC: How do I enable dashes in my urls?

前端 未结 9 1003
礼貌的吻别
礼貌的吻别 2020-11-28 21:52

I\'d like to have dashes separate words in my URLs. So instead of:

/MyController/MyAction

I\'d like:

/My-Controller/My-Act         


        
9条回答
  •  有刺的猬
    2020-11-28 22:37

    Asp.Net MVC 5 will support attribute routing, allowing more explicit control over route names. Sample usage will look like:

    [RoutePrefix("dogs-and-cats")]
    public class DogsAndCatsController : Controller
    {
        [HttpGet("living-together")]
        public ViewResult LivingTogether() { ... }
    
        [HttpPost("mass-hysteria")]
        public ViewResult MassHysteria() { }
    }
    

    To get this behavior for projects using Asp.Net MVC prior to v5, similar functionality can be found with the AttributeRouting project (also available as a nuget). In fact, Microsoft reached out to the author of AttributeRouting to help them with their implementation for MVC 5.

提交回复
热议问题