我正在构建一个 MCV4 Web API 应用程序,我想让用户向应用程序添加插件。它需要在不修改原始应用程序的情况下向 apicontrollers 添加操作。我想知道是否可以将操作添加为扩展方法,例如以下代码。
//controller in the original application
namespace MyApplication.Controllers
{
public class MyController : ApiController
{
//some action
}
}
//in user assembly
//user plug-in register route
config.Routes.MapHttpRoute(
name: "userAction",
routeTemplate: "api/{controller}/userAction/{id}",
defaults: new { controller = "MyController", action = "userAction" },
);
//also in user assembly
//extension method
using MyApplication.Controllers;
.........
public static class MyExtension
{
[HttpGet]
public static object userAction(this MyController c, int id)
{
return id;
}
}
ablove 代码当然不起作用,无法找到该操作。
有没有办法做这样的事情?