ASP.NET MVC enum argument in controller mapping

吃可爱长大的小学妹 提交于 2019-12-05 06:05:45

/Controller/MyMethod/All will actually work. The problem is with the default route, which will consider All to be the id route parameter, which doesn't line up with what your action is using as a parameter. It would actually work fine if your action signature was:

public ActionResult MyMethod(MyEnum id = MyEnum.Pending)

Since it will then bind All to the right thing.

You could add another route for this use-case, but you'll need to be careful that you don't just create another "default" route, which will take over. In other words, you'll have to fix part of the URL:

routes.MapRoute(
    "MyCustomRoute",
    "Controller/{action}/{myEnum}",
    new { controller = "Controller", action = "MyMethod", myEnum = MyEnum.Pending }
);

routes.MapRoute(
    "Default",
    "{controller}/{action}/{id}",
    new { controller = "Home", action = "Index", id = UrlParameter.Optional }
 );

Then, by the mere presence of the /Controller/ prefix to the route, it will use your custom route instead, and fill in All for the myEnum param, rather than hitting the default route and try to fill in id.

However, be advised that when using enums as route params, they must be exact matches. So, while /Controller/MyMethod/All will work, /Controller/MyMethod/all will not. To get around this, you'll have to create a custom model binder. I did a quick search and found the following article which may help you in that regard.

You can indeed. Do not change the default route "{controller}/{action}/{id}", but rather add one before the default. This new one needs to be fairly specific:

routes.MapRoute(
    "EnumRoute",
    "Controller/MyMethod/{myEnum}",
    new { controller = "Controller", action = "MyMethod", myEnum = UrlParameter.Optional }
);

What that basically says is "when you see request to literally Controller/MyMethod/whatever, use this controller and that method and pass whatever as parameter of the request". Note that actual controller does not necessary have to be what route says in the url, although it is a good idea to stick to that.

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