本文介绍了AttributeRouting - 从获取的RouteData操作名称的正确方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直在使用路由属性为我的行为方式最近开始和从的RouteData获取动作的名称(和/或ID)很努力。下面是我如何使用attrubues像这样的例子:

I have recently started using attribute routing for my action methods and am struggling in getting the action name (and/or id) from the RouteData. Below is an example of how I use the attrubues like so:

[Route( "Edit/{id:int}" )]
public ActionResult Edit( int id )
{
    ...
}

previously我用下面的方法的RouteData的扩展检索值

Previously I used the following method as an extension of RouteData to retrieve the value

public static string GetActionName( this RouteData routeData )
{
    return routeData.Values[ "Action" ] as string;
}

这用于返回操作名称(本例中的编辑)。现在,这个送花儿给人返回null。所述的RouteData字典的检查表明,这些数值似乎不再被放置在阵列的根,而是在键入一个项[0] =MS_DirectRouteMatches

This used to return the action name (in this example "edit"). Now this alway returns null. An inspection of the RouteData dictionary shows that these values seem to no longer be placed in the root of the array but rather in an item keyed "[0] = "MS_DirectRouteMatches".

我能够通过以下方式,但访问此值,由于我有限的这些值是如何理解填充我关注的是,对于例如那里有一个以上的比赛航线,code会在某些情况下摔倒。

I am able to access this value in the following manner but due to my limited understanding of how these values are populated I am concerned that, for e.g. routes where there are more than one match, this code will fall over in some cases.

public static string GetActionName( this RouteData routeData )
{
    if( routeData.Values.ContainsKey( "MS_DirectRouteMatches" ) )
        routeData = ( ( IList<RouteData> )routeData.Values[ "MS_DirectRouteMatches" ] )[ 0 ];

    return routeData.Values[ "Action" ] as string;
}

什么是AttributeRouting访问填充的RouteData值的正确方法是什么?

What is the correct way to access the RouteData values populated by AttributeRouting?

推荐答案

您的解决方案将在所有的工作,但一个案件。这可能是一个请求将不会匹配任何路线,所以你应该确保动作返回前项是否存在,或者您可能会得到一个异常。

Your solution will work in all but one case. It is possible that a request will not match any route, so you should ensure the action key exists before you return it, or you may get an exception.

public static string GetActionName( this RouteData routeData )
{
    if( routeData.Values.ContainsKey( "MS_DirectRouteMatches" ) )
        routeData = ( ( IEnumerable<RouteData> )routeData.Values["MS_DirectRouteMatches"] )[0];

    return routeData.Values.ContainsKey("action") ? 
        routeData.Values["action"] as string : 
        string.Empty;
}

这一个案件通常将请求传递给IIS,因此它可以返回404错误页面(或者自定义如果配置了),这基本上只是涵盖了自定义的404页面的情况下。

That one case typically passes the request to IIS so it can return the 404 page (or a custom one if configured), and this basically just covers the custom 404 page case.

我也会改变的IList 的IEnumerable ,因为你添加无关的方法列表。

I would also change IList to be IEnumerable, since you are adding nothing to the list in the method.

有没有这样的事情作为匹配多个路径(0或1路是唯一的可能性)的请求。在没有匹配的情况下, MS_DirectRouteMatches 将不存在。在它确实存在的情况下,存在在集合中值为1

There is no such thing as a request that matches more than one route (0 or 1 route are the only possibilities). In the case where there is no match, MS_DirectRouteMatches will not exist. In the case where it does exist, there is 1 value in the collection.

不幸的是,没有任何真正的保证集合中的值的数目将不会在以后的版本中改变一个以上的,但目前该成立。但是,因为没有一个更强大的方法来确定一个动作名称比这个,这是我们所坚持,直到下一个重大更改。

Unfortunately, there aren't really any guarantees that the number of values in the collection won't change to more than one in a future version, but for now that holds true. But since there isn't a more robust way to determine an action name than this, it is what we are stuck with until the next breaking change.

这篇关于AttributeRouting - 从获取的RouteData操作名称的正确方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-15 23:47