是否有可能使用的路由属性,可以改变路由表中的顺序? [英] Is it possible to change order of routes in routing table when using attribute routing?

查看:185
本文介绍了是否有可能使用的路由属性,可以改变路由表中的顺序?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所以,我在使用AreaRegistration使用属性路由交换的区域。我运行到这似乎由其中路由装入路由表中的顺序引起的问题。我已经解决了AreaRegistration问题通过加载在最后有问题的路径,因此,只有当所有其他路线没有匹配将其路线匹配。同属性的路由,这不会出现是可能的。我创建一个路由时,该命令的参数,但是这并不影响事情的命中路由表中,除了非常狭窄。

So, I'm switching an area over from using AreaRegistration to using Attribute Routing. I'm running into an issue which appears to be caused by the order in which routes are loaded into the routing table. I'd solved the issue in AreaRegistration by loading in the problematic route last, so that only if all other routes didn't match would that route be matched. With Attribute Routing, this doesn't appear to be possible. I have the Order parameter when creating a route, but this doesn't affect how things hit the routing table except very narrowly.

下面是在AreaRegistration文件我的路线:

Here's the route I have in the AreaRegistration file:

context.MapRoute(
    name: "ActionItems_home",
    url: "ActionItems/{group}/{statuses}/{overdueOnly}",
    defaults: new { controller = "Home", action = "Index", group = "All", statuses = "New,Open", overdueOnly = false },
    namespaces: new string[] { "IssueTracker.Areas.ActionItems.Controllers" }
    );

现在,当我尝试切换到该路由的属性来接近工作的唯一事情是:

Now, when I try to switch this to Attribute Routing the only thing that comes close to working is:

[Route("", Order = 4)]
[Route("{group:regex(^(?!Item|DecisionLogs))?}", Order = 3)]
[Route("{group:regex(^(?!Item|DecisionLogs))}/{statuses=New,Open?}", Order = 2)]
[Route("{group:regex(^(?!Item|DecisionLogs))}/{statuses=New,Open}/{overdueOnly:bool=false?}", Order = 1)]

请注意,我必须把正则表达式,否则该项目控制器不会被调用 - 相反,我最终被传递作为参数。但正则表达式并没有特别帮助的URL结束了的呈现方式。

Note that I have to put in the regex because otherwise the Item controller doesn't get called - instead, I end up with the string 'Item' being passed in as the group parameter. But the regex doesn't particularly help with how the URL's end up being rendered.

我会的的为可选参数是在URL pssed除非他们非默认燮$ P $。我已经试过指定参数可选,使用默认值,两者可选的默认值。他们没有似乎真的做的伎俩。

I would like for the optional parameters to be suppressed in the URL unless they are non-default. I've tried specifying the parameters as optional, with default values, and both optional and with default values. None of them seems to really do the trick.

目前的解决方案至少presents没有查询字符串的URL,但它们包括可选的参数,让事情难看。现在,我只是留在 AreaRegistration 文件和放大器要定义的令人震惊的路线;不饰它们与 [路线()] 部分。

The current solution at least presents a URL without a querystring, but they include the optional parameters and make things ugly. For now, I've simply left the egregious routes to be defined in AreaRegistration files & not decorated them with the [Route()] pieces.

推荐答案

您真正的问题是如何配置与属性的路由原来的路线。顺序的问题是只配置几条路代替一个的副作用。
为了达到您想要的配置,您可以创建自定义RouteAttribute,做任何你需要的内部。

Your real problem is how to configure your original route with Attribute Routing. The order problem is just a side effect of configuring several routes instead of one. To achieve your desired configuration, you can create a custom RouteAttribute and do whatever you need inside.

public class OptionalsRouteAttribute : RouteFactoryAttribute
{
    private object _defaults;

    public OptionalsRouteAttribute(string template, object defaults)
        : base(template)
    {
        Defaults = defaults;
    }

    [...]
}

您可以看到一个<一个href=\"http://weblogs.asp.net/jongalloway/looking-at-asp-net-mvc-5-1-and-web-api-2-1-part-2-attribute-routing-with-custom-constraints\"相对=nofollow>样本这里
而<一个href=\"http://sourcebrowser.io/Browse/ASP-NET-MVC/aspnetwebstack/src/Common/Routing/RouteFactoryAttribute.cs\"相对=nofollow>原RouteFactoryAttribute 来源,以供参考。

You can see a sample here And the original RouteFactoryAttribute source for reference

我恐怕没有时间,现在提供的实际执行情况,不过我希望这将带领您正确的方向。

I'm afraid I don't have time right now to provide the actual implementation myself, but I hope this will lead you to the right direction.

更新
我给这一个尝试,下面很简单的解决办法按预期工作。我的属性实现特定于您提供组,状态和参数overdueOnly样品,但你应该能够创建涵盖所有的情况下,更通用的解决方案(你还需要添加命名空间)

UPDATE I've given this a try and the following very simple solution works as expected. My attribute implementation is specific to the sample you provided with group, statuses and overdueOnly parameters, but you should be able to create a more generic solution that covers all your cases (you'll also need to add the namespace)

 public class OptionalsRouteAttribute : RouteFactoryAttribute
{
    public OptionalsRouteAttribute(string template, string group, string statuses, bool overdueOnly)
        : base(template)
    {
        var defaults = new RouteValueDictionary
        {
            {"group", @group},
            {"statuses", statuses},
            {"overdueOnly", overdueOnly}
        };
        Defaults = defaults;
    }

    public override RouteValueDictionary Defaults { get; }

}

然后在控制器:

 [OptionalsRoute("ActionItemsAttribute/{group}/{statuses}/{overdueOnly}", "All", "New,Open", false)]
    public ActionResult AttributeRouting(string group, string statuses, bool overdueOnly)
    {
        ViewBag.Message = $"Attribute Routing: Group [{@group}] - Statuses [{statuses}] - overdueOnly [{overdueOnly}]";
        return View("Index");
    }

和它的工作原理完全一样,你最初的路由配置,但使用的属性。

And it works exactly the same as your initial routing configuration, but using an attribute.

这篇关于是否有可能使用的路由属性,可以改变路由表中的顺序?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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