简体   繁体   English

更改 ASP.NET MVC 3 controller 路由行为

[英]Changing ASP.NET MVC 3 controller routing behavior

Let's say I have a few controllers with long names like VeryLongNameController.假设我有几个名称很长的控制器,例如 VeryLongNameController。

By default ASP.NET MVC3 will map ~/VeryLongName or ~/verylongname to this controller.默认情况下 ASP.NET MVC3 将 map ~/VeryLongName 或 ~/verylongname 到这个 controller。 However I don't like the use of capital names within the URL and would like it to map all long named controllers like ~/very-long-name instead.但是我不喜欢在 URL 中使用大写名称,并希望 map 所有长名称的控制器都像 ~/very-long-name 一样。

I know that it's possible to add custom routes one by one, but is there a way to change the default behavior?我知道可以一一添加自定义路由,但是有没有办法更改默认行为?

You can, you need to provide your own route handler implementing IRouterHandler , there's a good example here .您可以,您需要提供自己的实现IRouterHandler的路由处理程序,这里有一个很好的示例。

You can use an ActionName attribute specifically for an action method.. not a controller though您可以将 ActionName 属性专门用于操作方法。虽然不是 controller


[ActionName("an-action-with-long-name")]
public ActionResult AnActionWithLongName() {
  // ...
}

Also - I prefer to add a route for every controller/action method so I don't create any unexpected mappings (I unit test them as well too) - so this is one thing to consider.另外——我更喜欢为每个控制器/动作方法添加一个路由,这样我就不会创建任何意外的映射(我也对它们进行单元测试)——所以这是需要考虑的一件事。

I've investigated this a bit more, and got it working by making my own IHttpHandler and IRouteHandler, looking at the source for System.Web.Mvc.MvcHandler and System.Web.Mvc.MvcRouteHandler and basically copying and pasting and replacing how it resolves the controller name.我对此进行了更多调查,并通过制作自己的 IHttpHandler 和 IRouteHandler 使其工作,查看System.Web.Mvc.MvcHandlerSystem.Web.Mvc.MvcRouteHandler的源代码,并基本上复制和粘贴并替换它解析 controller 名称。 However I don't like this approach at all, as it feels too heavy-weight to redo the whole request processing pipe for a simple cosmetic task.但是我根本不喜欢这种方法,因为对于简单的修饰任务重做整个请求处理 pipe 感觉太重了。 Thus, I will go with adding manual routes for each controller which has two names (which there aren't that many).因此,我将 go 为每个具有两个名称(数量不多)的 controller 添加手动路线。

UPDATE: I've come with a much simpler solution, and that is done through overriding ControllerFactory.更新:我提供了一个更简单的解决方案,这是通过覆盖 ControllerFactory 来完成的。

public class ControllerFactory : DefaultControllerFactory
{
    public override IController CreateController(RequestContext requestContext, 
        string controllerName)
    {
        requestContext.RouteData.Values["action"] =
            requestContext.RouteData.Values["action"].ToString().Replace("-", "");
        return base.CreateController(requestContext, controllerName.Replace("-",""));
    }
}

My blog post about it: http://cangencer.wordpress.com/2011/05/27/better-looking-urls-in-asp-net-mvc-3/我的博客文章: http://cangencer.wordpress.com/2011/05/27/better-looking-urls-in-asp-net-mvc-3/

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM