ASP.NET MVC Routing: Clean Urls - from camel case to hyphenated words

[亡魂溺海] 提交于 2019-12-13 19:33:27

问题


I currently have an action defined in my controller:

    // GET: /schools/:cleanUrlName/data-loggers
    public ActionResult DataLoggers(string cleanUrlName)
    {
        return View();
    }

This works when I hit "/schools/brisbane-state-high-school/dataloggers", however - as per the comment - I want to access it via a slightly cleaner url (using hyphens): "/schools/brisbane-state-high-school/data-loggers". I know I could write a route to accomplish this, but I was hoping I wouldn't have to write a new route for every multi-worded action/controller. Is there a better way to address this?


回答1:


You can use the ActionNameAttribute to create an alias for your action name.

So you just need to annotate your multi worded actions:

[ActionName("data-loggers")]
public ActionResult DataLoggers(string cleanUrlName)
{
    return View("DataLoggers");
}

But because this affects also the view discovery therefore you need to return View("DataLoggers") so you are probably better with creating custom routes for your multi worded actions.



来源:https://stackoverflow.com/questions/18584503/asp-net-mvc-routing-clean-urls-from-camel-case-to-hyphenated-words

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!