HttpRouteBuilder - 它去了哪里,为什么?

时间:2013-10-22 22:28:31

标签: asp.net-web-api asp.net-web-api-routing

我将用于Web API 2的nuget包从RC1升级到5.0.0,并且发现曾经可以访问的HttpRouteBuilder是内置的。除此之外,HttpConfiguration.MapHttpAttributeRoutes不再存在重载,它将HttpRouteBuilder作为参数。为什么呢?

我正在使用它,它解决了我项目中的一个主要问题。我该怎么用?

背景: 我正在编写一个使用Web API 2属性路由的服务器。我实现了一个继承自HttpRouteBuilder的类,这样我就可以为每个URI注入一些额外的路径段。例如,如果默认路由构建器最终为 // myserver / user / update 创建路由,则我的路由构建器会将该路由修改为 // myserver / {instance} / user /更新即可。我希望这是自动完成的,这样我就不必在我的数百个HttpGet,HttpPost等属性的每一个中都坚持这一点。那么现在我如何处理这一重大变化?

1 个答案:

答案 0 :(得分:1)

这种内部结构打破了我正在努力的事情。

change set made on August 21st 2013对此修改this issue进行了此修改。根据该问题,删除功能的唯一原因是使Web Api更接近MVC的API。在我看来,这不是一个特别好的理由。

要解决我的问题,我实施了源自IHttpActionSelector的自定义ApiControllerActionSelector。我希望它不会成为我的最终解决方案,因为对于一个简单的事情它真的是太多的代码。这种方法也适用于您的问题。

在我的项目中,每个路径都需要根据找到的程序集进行修改。在遵循简化的代码中,每个路径都以/Api为前缀(在控制器的RoutePrefixAttribute之前,如果存在)。

实际IHttpActionSelector

public class PrefixWithApiControllerActionSelector : WrappingApiControllerActionSelector {
    protected override HttpActionDescriptor WrapHttpActionDescriptor(HttpActionDescriptor actionDescriptor) {
        if (actionDescriptor is ReflectedHttpActionDescriptor)
            return new PrefixWithApiReflectedHttpActionDescriptor((ReflectedHttpActionDescriptor)actionDescriptor);
        return actionDescriptor;
    }
}

public abstract class WrappingApiControllerActionSelector : ApiControllerActionSelector {
    protected abstract HttpActionDescriptor WrapHttpActionDescriptor(HttpActionDescriptor actionDescriptor);

    public override ILookup<string, HttpActionDescriptor> GetActionMapping(HttpControllerDescriptor controllerDescriptor) {
        return base.GetActionMapping(controllerDescriptor).SelectMany(grouping => {
            return grouping.Select(actionDescriptor => new KeyValuePair<string, HttpActionDescriptor>(grouping.Key, WrapHttpActionDescriptor(actionDescriptor)));
        }).ToLookup(_ => _.Key, _ => _.Value);
    }
}

改变路线的部分:

public class PrefixWithApiHttpRouteInfoProvider : WrappedHttpRouteInfoProvider {
    public PrefixWithApiHttpRouteInfoProvider(IHttpRouteInfoProvider infoProvider, HttpControllerDescriptor controllerDescriptor) : base(infoProvider, controllerDescriptor) { }

    public override string Template {
        get {
            var parts = new List<string>();
            parts.Add("Api");

            var prefix = ControllerDescriptor.GetCustomAttributes<RoutePrefixAttribute>().FirstOrDefault();
            if (prefix != null && !string.IsNullOrEmpty(prefix.Prefix)) {
                parts.Add(prefix.Prefix);
            }

            if (!string.IsNullOrEmpty(InfoProvider.Template)) {
                parts.Add(InfoProvider.Template);
            }

            var route = "~/" + string.Join("/", parts);
            if (route.Length > 2 && route.EndsWith("/", StringComparison.Ordinal)) {
                route = route.Substring(0, route.Length - 1);
            }
            return route;
        }
    }
}

public abstract class WrappedHttpRouteInfoProvider : IHttpRouteInfoProvider {
    private readonly IHttpRouteInfoProvider _infoProvider;
    private readonly HttpControllerDescriptor _controllerDescriptor;

    protected WrappedHttpRouteInfoProvider(IHttpRouteInfoProvider infoProvider, HttpControllerDescriptor controllerDescriptor) {
        _infoProvider = infoProvider;
        _controllerDescriptor = controllerDescriptor;
    }

    public virtual string Name {
        get { return InfoProvider.Name; }
    }

    public virtual string Template {
        get { return _infoProvider.Template; }
    }

    public virtual int Order {
        get { return InfoProvider.Order; }
    }

    protected HttpControllerDescriptor ControllerDescriptor {
        get { return _controllerDescriptor; }
    }

    protected IHttpRouteInfoProvider InfoProvider {
        get { return _infoProvider; }
    }
}

胶水:

public class PrefixWithApiReflectedHttpActionDescriptor : WrappedReflectedHttpActionDescriptor {
    public PrefixWithApiReflectedHttpActionDescriptor(ReflectedHttpActionDescriptor descriptor) : base(descriptor) {}

    public override Collection<T> GetCustomAttributes<T>(bool inherit) {
        if (typeof(T) == typeof(IHttpRouteInfoProvider)) {
            var attributes = Descriptor.GetCustomAttributes<T>(inherit).Cast<IHttpRouteInfoProvider>().Select(_ => new PrefixWithApiHttpRouteInfoProvider(_, Descriptor.ControllerDescriptor));
            return new Collection<T>(attributes.Cast<T>().ToList());
        }
        return Descriptor.GetCustomAttributes<T>(inherit);
    }

    public override Collection<T> GetCustomAttributes<T>() {
        if (typeof(T) == typeof(IHttpRouteInfoProvider)) {
            var attributes = Descriptor.GetCustomAttributes<T>().Cast<IHttpRouteInfoProvider>().Select(_ => new PrefixWithApiHttpRouteInfoProvider(_, Descriptor.ControllerDescriptor));
            return new Collection<T>(attributes.Cast<T>().ToList());
        }
        return Descriptor.GetCustomAttributes<T>();
    }
}

public abstract class WrappedReflectedHttpActionDescriptor : ReflectedHttpActionDescriptor {
    private readonly ReflectedHttpActionDescriptor _descriptor;

    protected WrappedReflectedHttpActionDescriptor(ReflectedHttpActionDescriptor descriptor) : base(descriptor.ControllerDescriptor, descriptor.MethodInfo) {
        _descriptor = descriptor;
    }

    public override HttpActionBinding ActionBinding {
        get { return Descriptor.ActionBinding; }
        set { Descriptor.ActionBinding = value; }
    }

    public override Collection<T> GetCustomAttributes<T>(bool inherit) {
        return Descriptor.GetCustomAttributes<T>(inherit);
    }

    public override Collection<T> GetCustomAttributes<T>() {
        return Descriptor.GetCustomAttributes<T>();
    }

    public override Collection<System.Web.Http.Filters.FilterInfo> GetFilterPipeline() {
        return Descriptor.GetFilterPipeline();
    }

    public override Collection<System.Web.Http.Filters.IFilter> GetFilters() {
        return Descriptor.GetFilters();
    }

    public override System.Collections.Concurrent.ConcurrentDictionary<object, object> Properties {
        get { return Descriptor.Properties; }
    }

    public override IActionResultConverter ResultConverter {
        get { return Descriptor.ResultConverter; }
    }

    public override Collection<HttpMethod> SupportedHttpMethods {
        get { return Descriptor.SupportedHttpMethods; }
    }

    public override Collection<HttpParameterDescriptor> GetParameters() {
        return Descriptor.GetParameters();
    }

    public override Task<object> ExecuteAsync(HttpControllerContext controllerContext, IDictionary<string, object> arguments, CancellationToken cancellationToken) {
        return Descriptor.ExecuteAsync(controllerContext, arguments, cancellationToken);
    }

    public override string ActionName {
        get { return Descriptor.ActionName; }
    }

    public override Type ReturnType {
        get { return Descriptor.ReturnType; }
    }

    protected ReflectedHttpActionDescriptor Descriptor {
        get { return _descriptor; }
    }
}

要使用此功能,只需将IHttpActionSelector服务替换为配置中的PrefixWithApiControllerActionSelector

如果您找到更清洁的做事方式,请发布您的解决方案!