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

前端 未结 9 1061
礼貌的吻别
礼貌的吻别 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:56

    If you have access to the IIS URL Rewrite module ( http://blogs.iis.net/ruslany/archive/2009/04/08/10-url-rewriting-tips-and-tricks.aspx ), you can simply rewrite the URLs.

    Requests to /my-controller/my-action can be rewritten to /mycontroller/myaction and then there is no need to write custom handlers or anything else. Visitors get pretty urls and you get ones MVC can understand.

    Here's an example for one controller and action, but you could modify this to be a more generic solution:

    
      
        
          
          
        
      
    
    

    The possible downside to this is you'll have to switch your project to use IIS Express or IIS for rewrites to work during development.

提交回复
热议问题