- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我们有一个 IRouteConstraint
被检查得比它应该的多得多。经过进一步测试,看起来 [Route]
上的 Order
被路由约束忽略了。
例如,如果我有以下约束:
public class TestConstraint : IRouteConstraint {
public bool Match(
HttpContextBase httpContext,
Route route,
string parameterName,
RouteValueDictionary values,
RouteDirection routeDirection
) {
Debug.WriteLine("TestConstraint");
return true;
}
}
然后连接起来:
constraintResolver.ConstraintMap.Add("testConstraint", typeof(TestConstraint));
并且有以下路由:
public partial class HomeController {
[Route("test/0", Order = 1)]
public ActionResult Test0() {
return Content("Test0");
}
[Route("{someParam}/{test:testConstraint}", Order = 10)]
public ActionResult Test1() {
return Content("Test1");
}
}
然后请求http://localhost/test/0
,它会返回正确的内容(Test0
),但是TestContraint.Match ()
仍然执行。
我认为只有在 RouteTable
中遇到路由时才会执行路由约束,但它似乎在每个 可以匹配 的请求上运行它>[路线]
模式。
如果它有所作为,我们使用的是 ASP.NET MVC v5.2.4。
最佳答案
在 ASP.NET MVC 管道中,路由阶段和调用 Controller 操作的选择阶段是分开的。在路由阶段,您不能只选择第一个匹配操作并停止进一步查找。找到的 Action (严格来说是方法)可以在后期过滤。例如,它可能不满足应用的 Action 选择器(例如 NonAction
属性)。
这就是为什么基本的 Action 选择算法如下:
现在有以下选项:
如果您对相应的 ASP.NET MVC 源代码感兴趣,这里有一些引用资料:
IRouteConstraint.Match()
由 System.Web.Routing.Route
中的 ProcessConstraint()
方法调用。调用堆栈中最近的方法,在路由集合级别上运行,是 System.Web.Mvc.Routing.RouteCollectionRoute
类中的 GetRouteData()
方法:
这是它的 source code :
public override RouteData GetRouteData(HttpContextBase httpContext)
{
List<RouteData> matches = new List<RouteData>();
foreach (RouteBase route in _subRoutes)
{
var match = route.GetRouteData(httpContext);
if (match != null)
{
matches.Add(match);
}
}
return CreateDirectRouteMatch(this, matches);
}
如您所见,当找到匹配的路由时,循环不会中断。
应用 Action 选择器、执行排序和选择 Action 候选的代码位于 DirectRouteCandidate.SelectBestCandidate()
( source code ) 中:
public static DirectRouteCandidate SelectBestCandidate(List<DirectRouteCandidate> candidates, ControllerContext controllerContext)
{
Debug.Assert(controllerContext != null);
Debug.Assert(candidates != null);
// These filters will allow actions to opt-out of execution via the provided public extensibility points.
List<DirectRouteCandidate> filteredByActionName = ApplyActionNameFilters(candidates, controllerContext);
List<DirectRouteCandidate> applicableCandidates = ApplyActionSelectors(filteredByActionName, controllerContext);
// At this point all of the remaining actions are applicable - now we're just trying to find the
// most specific match.
//
// Order is first, because it's the 'override' to our algorithm
List<DirectRouteCandidate> filteredByOrder = FilterByOrder(applicableCandidates);
List<DirectRouteCandidate> filteredByPrecedence = FilterByPrecedence(filteredByOrder);
if (filteredByPrecedence.Count == 0)
{
return null;
}
else if (filteredByPrecedence.Count == 1)
{
return filteredByPrecedence[0];
}
else
{
throw CreateAmbiguiousMatchException(candidates);
}
}
关于asp.net-mvc - RouteAttribute 的 Order 在 IRouteConstraint 上被完全忽略,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49869484/
我目前在 DTO 之间有继承,只要我为每个具体(叶节点)DTO 定义了一个 [Route],而不是在某个父类(super class)上,它就可以很好地工作。到目前为止,父类(super class)
我有一个带有两个必需参数和一些可选参数的操作: [HttpGet] public IHttpActionResult GetUsers(DateTime dateFrom, DateTime date
在 ASP.NET Core (v 2.1.5) 中,您可以创建 Controller ,而无需让它们继承 Controller 类(如您所知)。如果这样做,您必须使用 RouteAttribute
我一直在使用 WebApi,我试图了解使用路由属性与让 WebApi 根据函数名称自动创建路由的区别。我有很多这种形式的 Controller : public class MyController
这是我的 Controller 操作: HttpGet // GET: ControllerName/Create [Route("CreateDocument/{personId}")] publi
我有这门课: [RoutePrefix("api/v2/Foo")] public class SomeController : BaseController { [Route("")]
这是我的 Global.asax protected void Application_Start() { RegisterRoutes(); } pr
我以为是在网络上进行简单的搜索,结果却不止于此。 最接近解决方案的是第一个可以使用属性进行路由的解决方案:AttributeRouting not working with HttpConfigura
我已经使用 NuGet 包管理器将 ASP.NET web API self host 添加到我的项目(一个 Windows 服务项目)Nuget 添加了那些库 ASP.NET Web API 自托管
我们有一个 IRouteConstraint 被检查得比它应该的多得多。经过进一步测试,看起来 [Route] 上的 Order 被路由约束忽略了。 例如,如果我有以下约束: public class
我想知道 ServiceStack 注册路由的最佳实践是什么。 在 AppHost 中使用 Routes.Add 定义路由 在 DTO 上使用 RouteAttiribute 装饰器定义路由 我读过一
我正在使用 MVC 5,我正在尝试通过 System.Web.Mvc.RouteAttribute 完成路由。 所以大多数操作都有效,但一个无效。 我创建了一个Delete-action 和一个Edi
我需要制作一个支持多个 ASP.NET Core, Web API 动词的 HttpGet ,唯一的区别是查询字符串,但似乎查询字符串不能成为路由模板的一部分——这是真的吗? 路由模板非常相似,实际上
RouteAttribute("abc", Order = 2) 顺序似乎只在 Controller 中受到尊重:当在同一 Controller 中定义以下方法时,我得到预期的行为,即 Method1
我是一名优秀的程序员,十分优秀!