繁体   English   中英

指定在运行时执行哪个api控制器

[英]Specify which api controller to execute at runtime

我在两个不同的程序集中(在两个不同的项目中)有两个同名的控制器(假设它是XController )。

一种只是返回模拟数据,第二种是真正的交易。

如果我尝试访问控制器http(s)://.../api/X ,当然会出现异常,因为找到了两个名称与uri匹配的控制器。

我的目标是能够通过一个或另一个控制器轻松地运行Web api。

我知道我可以将这些类包装在#if <symbol> ... #endif但是每次我想从一个XController切换到另一个XController ,都必须对两个项目进行更改。

我想我可以从IHttpControllerSelector工作,但也许那是牵强的(也许不是,我问)。

我该如何实现?

Microsoft建议的实现是使用涉及“命名空间”的自定义路由,并使用IHttpControllerSelector的自定义配置(正如您在问题中所建议的那样)。 这段代码是直接从链接的源代码派生的,我只是将其放在此处作为对以后任何路人的直接引用:

    // used in your WebApiConfig class
    config.Routes.MapHttpRoute(
        name: "DefaultApi",
        routeTemplate: "api/{namespace}/{controller}/{id}",
        defaults: new { id = RouteParameter.Optional }
    );

    config.Services.Replace(typeof(IHttpControllerSelector), new NamespaceHttpControllerSelector(config));

并实现NamespaceHttControllerSelector

public class NamespaceHttpControllerSelector : IHttpControllerSelector
{
    private const string NamespaceKey = "namespace";
    private const string ControllerKey = "controller";

    private readonly HttpConfiguration _configuration;
    private readonly Lazy<Dictionary<string, HttpControllerDescriptor>> _controllers;
    private readonly HashSet<string> _duplicates;

    public NamespaceHttpControllerSelector(HttpConfiguration config)
    {
        _configuration = config;
        _duplicates = new HashSet<string>(StringComparer.OrdinalIgnoreCase);
        _controllers = new Lazy<Dictionary<string, HttpControllerDescriptor>>(InitializeControllerDictionary);
    }

    private Dictionary<string, HttpControllerDescriptor> InitializeControllerDictionary()
    {
        var dictionary = new Dictionary<string, HttpControllerDescriptor>(StringComparer.OrdinalIgnoreCase);

        // Create a lookup table where key is "namespace.controller". The value of "namespace" is the last
        // segment of the full namespace. For example:
        // MyApplication.Controllers.V1.ProductsController => "V1.Products"
        IAssembliesResolver assembliesResolver = _configuration.Services.GetAssembliesResolver();
        IHttpControllerTypeResolver controllersResolver = _configuration.Services.GetHttpControllerTypeResolver();

        ICollection<Type> controllerTypes = controllersResolver.GetControllerTypes(assembliesResolver);

        foreach (Type t in controllerTypes)
        {
            var segments = t.Namespace.Split(Type.Delimiter);

            // For the dictionary key, strip "Controller" from the end of the type name.
            // This matches the behavior of DefaultHttpControllerSelector.
            var controllerName = t.Name.Remove(t.Name.Length - DefaultHttpControllerSelector.ControllerSuffix.Length);

            var key = String.Format(CultureInfo.InvariantCulture, "{0}.{1}", segments[segments.Length - 1], controllerName);

            // Check for duplicate keys.
            if (dictionary.Keys.Contains(key))
            {
                _duplicates.Add(key);
            }
            else
            {
                dictionary[key] = new HttpControllerDescriptor(_configuration, t.Name, t);  
            }
        }

        // Remove any duplicates from the dictionary, because these create ambiguous matches. 
        // For example, "Foo.V1.ProductsController" and "Bar.V1.ProductsController" both map to "v1.products".
        foreach (string s in _duplicates)
        {
            dictionary.Remove(s);
        }
        return dictionary;
    }

    // Get a value from the route data, if present.
    private static T GetRouteVariable<T>(IHttpRouteData routeData, string name)
    {
        object result = null;
        if (routeData.Values.TryGetValue(name, out result))
        {
            return (T)result;
        }
        return default(T);
    }

    public HttpControllerDescriptor SelectController(HttpRequestMessage request)
    {
        IHttpRouteData routeData = request.GetRouteData();
        if (routeData == null)
        {
            throw new HttpResponseException(HttpStatusCode.NotFound);
        }

        // Get the namespace and controller variables from the route data.
        string namespaceName = GetRouteVariable<string>(routeData, NamespaceKey);
        if (namespaceName == null)
        {
            throw new HttpResponseException(HttpStatusCode.NotFound);
        }

        string controllerName = GetRouteVariable<string>(routeData, ControllerKey);
        if (controllerName == null)
        {
            throw new HttpResponseException(HttpStatusCode.NotFound);
        }

        // Find a matching controller.
        string key = String.Format(CultureInfo.InvariantCulture, "{0}.{1}", namespaceName, controllerName);

        HttpControllerDescriptor controllerDescriptor;
        if (_controllers.Value.TryGetValue(key, out controllerDescriptor))
        {
            return controllerDescriptor;
        }
        else if (_duplicates.Contains(key))
        {
            throw new HttpResponseException(
                request.CreateErrorResponse(HttpStatusCode.InternalServerError,
                "Multiple controllers were found that match this request."));
        }
        else
        {
            throw new HttpResponseException(HttpStatusCode.NotFound);
        }
    }

    public IDictionary<string, HttpControllerDescriptor> GetControllerMapping()
    {
        return _controllers.Value;
    }
}

我提供了这种解决方案,但也许它具有一些我无法忽略的副作用。 有人可以告诉我有什么问题吗?

    protected void Application_Start()
    {
        var specificControllerTypes = new[]
        {
            typeof(Mocks.XController)
        };
        var config = GlobalConfiguration.Configuration;
        config.Services.Replace(
            typeof(IHttpControllerTypeResolver),
            new DefaultHttpControllerTypeResolver(type => 
                type.IsVisible &&
                !type.IsAbstract &&
                typeof(IHttpController).IsAssignableFrom(type) &&
                type.Name.EndsWith(DefaultHttpControllerSelector.ControllerSuffix) &&
                !specificControllerTypes.Any(t => t != type && t.Name == type.Name)
            )
        );

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM