添加Web API控制器,​​以不同的装配 [英] Add Web API Controller to different assembly

查看:166
本文介绍了添加Web API控制器,​​以不同的装配的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我如何添加Web API控制器到不同的组件? 我想在Web API控制器从我的MVC应用程序中分离出来。

How can I add Web Api Controller to a different assembly? I want to separate the web API controllers from my MVC application.

推荐答案

的WebAPI做开箱。

WebApi does that out the box.

该DefaultAssembliesResolver类是用来加载组件,以寻找ApiControllers之一。这里是源$ C ​​$ C:

The DefaultAssembliesResolver class is the one used to load the assemblies to look for ApiControllers. Here is the source code:

  public class DefaultAssembliesResolver : IAssembliesResolver {    
    public virtual ICollection<Assembly> GetAssemblies() {
      return AppDomain.CurrentDomain.GetAssemblies().ToList();
    }
  }

诀窍就是一个web.api服务的第一次调用之前,你的程序集加载到AppDomain中。

The trick is to load your assembly to the AppDomain before the first call to a web.api service.

调用类的任何方法在目标装配或简单地从它加载一个类型。例如:

Call any method of a class in the target assembly or simply load a type from it. Ex:

var x = typeof (YourApiControllerInAnotherAssembly);

如果您正在使用的WebAPI的是旧版本,尝试实施IAssembliesResolver,改变它的默认实现是这样的:

If you're using an older version of WebApi, try implementing IAssembliesResolver and changing the default implementation of it like this:

GlobalConfiguration.Configuration.Services.Replace(typeof(IAssembliesResolver), 
                                                   new YourCustomAssemblyResolver());

这篇关于添加Web API控制器,​​以不同的装配的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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