相关疑难解决方法(0)

模块化ASP.NET Web API:如何在运行时向Web API添加/删除路由

我正在尝试设计模块化Web API应用程序(它不是MVC应用程序!),其中管理员角色的用户可以在不重新启动ASP.NET应用程序的情况下添加或删除模块.

  • 模块:每个模块都是一个程序集(.dll文件),它至少包含一个派生自的类ApiController.
  • 路由基于ASP.NET Web API 2中的"属性路由"
  • 模块(组件)的生产不在本问题的范围内.
  • 模块(程序集文件)被复制到项目根目录中的〜/ plugins /文件夹中/从中删除.这个过程也不属于这个问题的范围.
  • 主要的ASP.NET Web API项目基本上只有一个控制器来管理(添加/删除)模块.其他控制器将作为模块添加.

因此,主Web API项目中唯一的控制器是:

[RoutePrefix("api/modules")]
public class ModulesController : ApiController
{
    private ModuleService _moduleService = new ModuleService();

    // GET: api/Modules
    [Route]
    public IEnumerable<string> Get()
    {
        return _moduleService.Get().Select(a => a.FullName);
    }

    // POST: api/Modules/{moduleName}
    [Route("{id}")]
    public void Post(string id)
    {
        Assembly _assembly;
        var result = _moduleService.TryLoad(id, out _assembly);

        if(!result) throw new Exception("problem loading " + id);

        // Refresh routs or add the new rout
        Configuration.Routes.Clear();
        Configuration.MapHttpAttributeRoutes(); …
Run Code Online (Sandbox Code Playgroud)

asp.net-web-api asp.net-web-api-routing asp.net-web-api2

7
推荐指数
1
解决办法
3795
查看次数