基于web api 2属性的路由生成

时间:2013-10-31 20:21:57

标签: c# asp.net-mvc-5 asp.net-web-api-routing

我一直在搞乱MVC5和WebApi2。有一点似乎有一个基于约定的RouteAttributes自动名称 - “ControllerName.ActionName”。我有一个很大的API与许多ApiControllers和使用属性定义的自定义路由。我可以直接使用网址并且效果很好,ApiExplorer可以很好地使用它。

然后我到了需要生成链接的地步,以及我的dto对象中的某些字段作为更新URL。我试着打电话:

  

Url.Link(“”,new {controller =“...”,action =“...”,[其他数据...]})

但它使用默认的全局路由定义,该路由不可用。

是否无法为没有使用UrlHelper.Link定义名称的基于属性的路线生成链接?

感谢任何意见,谢谢。

2 个答案:

答案 0 :(得分:1)

Route Names怎么样?也许你可以像在视图中那样暴露你的DTO中的那些。

<强>控制器

[Route("menu", Name = "mainmenu")]
public ActionResult MainMenu() { ... }`

查看

<a href="@Url.RouteUrl("mainmenu")">Main menu</a>

答案 1 :(得分:1)

使用algorithms described here,我选择使用ApiExplorer来获取与给定值集匹配的路线。

使用示例:

[RoutePrefix( "api/v2/test" )]
public class EntityController : ApiController {
    [Route( "" )]
    public IEnumerable<Entity> GetAll() {
        // ...
    }

    [Route( "{id:int}" )]
    public Entity Get( int id ) {
        // ...
    }

    // ... stuff

    [HttpGet]
    [Route( "{id:int}/children" )]
    public IEnumerable[Child] Children( int id ) {
        // ...
    }
}

///
/// elsewhere
///

// outputs: api/v2/test/5
request.HttpRouteUrl( HttpMethod.Get, new {
    controller = "entity",
    id = 5
} )

// outputs: api/v2/test/5/children
request.HttpRouteUrl( HttpMethod.Get, new {
    controller = "entity",
    action = "children",
    id = 5
} )

以下是实施:

public static class HttpRouteUrlExtension {
    private const string HttpRouteKey = "httproute";

    private static readonly Type[] SimpleTypes = new[] {
        typeof (DateTime), 
        typeof (Decimal), 
        typeof (Guid), 
        typeof (string), 
        typeof (TimeSpan)
    };

    public static string HttpRouteUrl( this HttpRequestMessage request, HttpMethod method, object routeValues ) {
        return HttpRouteUrl( request, method, new HttpRouteValueDictionary( routeValues ) );
    }

    public static string HttpRouteUrl( this HttpRequestMessage request, HttpMethod method, IDictionary<string, object> routeValues ) {
        if ( routeValues == null ) {
            throw new ArgumentNullException( "routeValues" );
        }

        if ( !routeValues.ContainsKey( "controller" ) ) {
            throw new ArgumentException( "'controller' key must be provided", "routeValues" );
        }

        routeValues = new HttpRouteValueDictionary( routeValues );
        if ( !routeValues.ContainsKey( HttpRouteKey ) ) {
            routeValues.Add( HttpRouteKey, true );
        }

        string controllerName = routeValues[ "controller" ].ToString();
        routeValues.Remove( "controller" );

        string actionName = string.Empty;
        if ( routeValues.ContainsKey( "action" ) ) {
            actionName = routeValues[ "action" ].ToString();
            routeValues.Remove( "action" );
        }

        IHttpRoute[] matchedRoutes = request.GetConfiguration().Services
                                            .GetApiExplorer().ApiDescriptions
                                            .Where( x => x.ActionDescriptor.ControllerDescriptor.ControllerName.Equals( controllerName, StringComparison.OrdinalIgnoreCase ) )
                                            .Where( x => x.ActionDescriptor.SupportedHttpMethods.Contains( method ) )
                                            .Where( x => string.IsNullOrEmpty( actionName ) || x.ActionDescriptor.ActionName.Equals( actionName, StringComparison.OrdinalIgnoreCase ) )
                                            .Select( x => new {
                                                route = x.Route,
                                                matches = x.ActionDescriptor.GetParameters()
                                                           .Count( p => ( !p.IsOptional ) &&
                                                                   ( p.ParameterType.IsPrimitive || SimpleTypes.Contains( p.ParameterType ) ) &&
                                                                   ( routeValues.ContainsKey( p.ParameterName ) ) &&
                                                                   ( routeValues[ p.ParameterName ].GetType() == p.ParameterType ) )
                                            } )
                                            .Where(x => x.matches > 0)
                                            .OrderBy( x => x.route.DataTokens[ "order" ] )
                                            .ThenBy( x => x.route.DataTokens[ "precedence" ] )
                                            .ThenByDescending( x => x.matches )
                                            .Select( x => x.route )
                                            .ToArray();

        if ( matchedRoutes.Length > 0 ) {
            IHttpVirtualPathData pathData = matchedRoutes[ 0 ].GetVirtualPath( request, routeValues );

            if ( pathData != null ) {
                return new Uri( new Uri( httpRequestMessage.RequestUri.GetLeftPart( UriPartial.Authority ) ), pathData.VirtualPath ).AbsoluteUri;
            }
        }

        return null;
    }
}