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