ASP.NET MVC 5属性的路由不能正确解决途径 [英] ASP.NET MVC 5 Attribute Routing Failing To Resolve Route Correctly

查看:124
本文介绍了ASP.NET MVC 5属性的路由不能正确解决途径的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在我的控制器的以下路线。一送和两个后的行动。所有具有相同动作名称。这两个动作后使用MultipleButton属性作为解释<一个分化href=\"http://stackoverflow.com/questions/442704/how-do-you-handle-multiple-submit-buttons-in-asp-net-mvc-framework\">here:

  [路线preFIX(事件)]
公共密封类IncidentsController:控制器
{
    [HTTPGET,路线(行动/ {ID:INT} / {?错误})]
    公共异步任务&LT;&的ActionResult GT;行动(INT?ID,字符串错误= NULL)    [HttpPost,路线(行动),ActionName(行动),ValidateAntiForgeryToken]
    [MultipleButton(名称=行动,参数=接受)]
    公共异步任务&LT;&的ActionResult GT; ActionAccept(IncidentActionViewModel事件)    [HttpPost,路线(行动),ActionName(行动),ValidateAntiForgeryToken]
    [MultipleButton(名称=行动,参数=拒绝)]
    公共异步任务&LT;&的ActionResult GT; ActionReject(IncidentActionViewModel事件)
}@ Url.Action(动作,事件,新的{ID = 10})

上面的路线被呈现为如下所示。导航到该URL工作,但如果我改变从可空的'身份证'参数为int我开始得到错误。


  

/事件/行动?ID = 10


应该呈现如下图所示,如果我改变了'身份证'参数为int类型,我应该不会得到错误:


  

/事件/动作/ 10


我在做什么错了?

更新

下面是我的路线报名详情:

  routes.IgnoreRoute({}资源个.axd / {*} PATHINFO);routes.MapMvcAttributeRoutes();routes.MapRoute(
    名称:默认,
    网址:{控制器} / {行动} / {ID}
    默认:新{控制器=家,行动=索引,ID = UrlParameter.Optional});


解决方案

作为一个工作的时候,我发现我可以使用,而不是依靠ASP.NET MVC命名路由解析使用的控制器和动作名称的路由。所以,我结束了以下code:

  [路线preFIX(事件)]
公共密封类IncidentsController:控制器
{
    [HTTPGET,路线(行动/ {ID:INT} {?错误} /,名称=IncidentsGetAction)]
    公共异步任务&LT;&的ActionResult GT;行动(INT?ID,字符串错误= NULL)    [HttpPost,路线(行动,名称=IncidentsPostActionAccept),ActionName(行动),ValidateAntiForgeryToken]
    [MultipleButton(名称=行动,参数=接受)]
    公共异步任务&LT;&的ActionResult GT; ActionAccept(IncidentActionViewModel事件)    [HttpPost,路线(行动,名称=IncidentsPostActionReject),ActionName(行动),ValidateAntiForgeryToken]
    [MultipleButton(名称=行动,参数=拒绝)]
    公共异步任务&LT;&的ActionResult GT; ActionReject(IncidentActionViewModel事件)
}//生成一个URL
@ Url.RouteUrl(IncidentsGetAction,新的{ID = 10})
//生成一个链接到一个网址
@ Html.RouteLink(链接文本,IncidentsGetAction,新的{ID = 10})

该方法具有更好的性能,MVC没有解决这个URL,就可以直接到它。它也更容易理解和减少错误引起的,你正在明确你想要使用的路线的机会。

我个人就不见了我的所有路由转换到名​​为途径和控制器和行动解决路由已经放弃。它仍然是有趣的,看看为什么会出现上述问题。

I have the following routes in my controller. One Get and two post actions. All have the same action name. The two post actions are differentiated using the MultipleButton attribute as explained here:

[RoutePrefix("incidents")]
public sealed class IncidentsController : Controller
{    
    [HttpGet, Route("action/{id:int?}/{error?}")]
    public async Task<ActionResult> Action(int? id, string error = null)

    [HttpPost, Route("action"), ActionName("Action"), ValidateAntiForgeryToken]
    [MultipleButton(Name = "action", Argument = "Accept")]
    public async Task<ActionResult> ActionAccept(IncidentActionViewModel incident)

    [HttpPost, Route("action"), ActionName("Action"), ValidateAntiForgeryToken]
    [MultipleButton(Name = "action", Argument = "Reject")]
    public async Task<ActionResult> ActionReject(IncidentActionViewModel incident)
}

@Url.Action("Action", "Incidents", new { id = 10 })

The above route gets rendered as shown below. Navigating to this URL works but if I change the 'id' parameter from Nullable to int I start to get errors.

/incidents/action?id=10

It should be rendering as shown below and I should not get errors if I change the 'id' parameter to type int:

/incidents/action/10

What am I doing wrong?

UPDATE

Here are my route registration details:

routes.IgnoreRoute("{resource}.axd/{*pathInfo}");

routes.MapMvcAttributeRoutes();

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

解决方案

As a work around, I found that I could use named routes instead of relying on ASP.NET MVC to resolve the routes using the controller and action names. So I ended up with the following code:

[RoutePrefix("incidents")]
public sealed class IncidentsController : Controller
{    
    [HttpGet, Route("action/{id:int?}/{error?}", Name = "IncidentsGetAction")]
    public async Task<ActionResult> Action(int? id, string error = null)

    [HttpPost, Route("action", Name = "IncidentsPostActionAccept"), ActionName("Action"), ValidateAntiForgeryToken]
    [MultipleButton(Name = "action", Argument = "Accept")]
    public async Task<ActionResult> ActionAccept(IncidentActionViewModel incident)

    [HttpPost, Route("action", Name = "IncidentsPostActionReject"), ActionName("Action"), ValidateAntiForgeryToken]
    [MultipleButton(Name = "action", Argument = "Reject")]
    public async Task<ActionResult> ActionReject(IncidentActionViewModel incident)
}

// Generate a URL
@Url.RouteUrl("IncidentsGetAction", new { id = 10 })
// Generate a link to a URL
@Html.RouteLink("Link Text", "IncidentsGetAction", new { id = 10 })

This approach has better performance as, MVC does not have to resolve the URL, it can go to it directly. It is also easier to understand and reduces the chances of bugs arising as you are being explicit about the route you want to use.

I personally have gone as far as converting all my routes to named routes and have abandoned using controller and action resolved routing. It would still be interesting to see why the above problem occurs.

这篇关于ASP.NET MVC 5属性的路由不能正确解决途径的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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