两条不同路线上的MVC路线属性错误 [英] MVC Route Attribute error on two different routes

查看:74
本文介绍了两条不同路线上的MVC路线属性错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在MVC 5.2中遇到属性路由错误.错误是

I'm getting an attribute routing error with MVC 5.2. The error is

找到与URL匹配的多种控制器类型.如果多个控制器上的属性路由与请求的URL匹配,则会发生这种情况.

Multiple controller types were found that match the URL. This can happen if attribute routes on multiple controllers match the requested URL.

有问题的路线是

[Route("{classname}/{id:int}")]
[Route("Edit/{id:int}")]

URL/Edit/123引发错误,而URL/someword/123 不引发错误

The url /Edit/123 throws the error, while the url /someword/123 does not throw the error

鉴于Edit/123比someword/123更具体,为什么会在/Edit/123上引发错误?

Given that Edit/123 is more specific than someword/123 why would it throw an error on /Edit/123?

谢谢

约翰

推荐答案

路由框架不会对您可能打算使用的路由进行判断(实际上是其中包含Edit的路由,因此显然我希望使用该路由).它所看到的只是它有两条与它具有的URL相匹配的路由,并且举起了手.

The routing framework doesn't make judgments about what route you probably intended (there's actually a route with Edit in it, so obviously I want that one). All it sees is that it has two routes which match the URL it has, and it throws its hands up.

只要您永远不需要编辑"作为classname的值,以下类似方法就可以解决歧义:

Something like the following should fix the ambiguity, as long as you never need "Edit" as a value for classname:

[Route("{classname:regex(^(?!Edit)$)}/{id:int}")]

这篇关于两条不同路线上的MVC路线属性错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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