Asp.Net MVC:我如何能够在我的网址破折号? [英] Asp.Net MVC: How do I enable dashes in my urls?

查看:111
本文介绍了Asp.Net MVC:我如何能够在我的网址破折号?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想有单独划线的话我的网址。因此,而不是:

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

/MyController/MyAction

我想:

/My-Controller/My-Action

这可能吗?

推荐答案

您可以使用ActionName属性,像这样:

You can use the ActionName attribute like so:

[ActionName("My-Action")]
public ActionResult MyAction() {
    return View();
}

请注意,您将需要再打电话给你的视图文件MY-Action.cshtml(或适当延长)。您还需要在任何Html.ActionLink方法参考我的行动。

Note that you will then need to call your View file "My-Action.cshtml" (or appropriate extension). You will also need to reference "my-action" in any Html.ActionLink methods.

有不是控制器这样一个简单的解决办法。

There isn't such a simple solution for controllers.

现在有MVC5,属性路由已被吸收到项目中。您现在可以使用:

Now with MVC5, Attribute Routing has been absorbed into the project. You can now use:

[Route("My-Action")]

在操作方法。

有关控制器,你可以申请一个路线preFIX 属性将被应用到该控制器的所有的操作方法:

For controllers, you can apply a RoutePrefix attribute which will be applied to all action methods in that controller:

[RoutePrefix("my-controller")]

对使用路线preFIX 的好处就是URL的参数也将被向下传递到任何操作方法。

One of the benefits of using RoutePrefix is URL parameters will also be passed down to any action methods.

[RoutePrefix("clients/{clientId:int}")]
public class ClientsController : Controller .....

剪断..

[Route("edit-client")]
public ActionResult Edit(int clientId) // will match /clients/123/edit-client

这篇关于Asp.Net MVC:我如何能够在我的网址破折号?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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