ASP.NET MVC路由:清洁的url - 骆驼情况下连字符的单词 [英] ASP.NET MVC Routing: Clean Urls - from camel case to hyphenated words

查看:208
本文介绍了ASP.NET MVC路由:清洁的url - 骆驼情况下连字符的单词的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我现在已经在我的控制器中定义的操作:

I currently have an action defined in my controller:

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

这工作时,我打/学校/布里斯班的状态高中/数据记录器,然而 - 按照注释 - 我想(用连字符),通过一个稍微干净的URL来访问它:/学校/布里斯班-state-高中/数据记录器。我知道我可以写做到这一点的路线,但我希望我不会写,每多措辞动作/控制器的新路线。有没有更好的办法来解决这个问题?

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?

推荐答案

您可以使用 ActionNameAttribute 以创建你的动作名称的别名。

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.

这篇关于ASP.NET MVC路由:清洁的url - 骆驼情况下连字符的单词的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆