如何将传入路由字符串(例如 Products/GetProduct)映射到将被调用的 Controller /方法(ProductsController、GetProduct 方法)?
我的目标是检查 ProductsContoller 的实例以找到放置在 GetProduct 方法上的自定义属性。
一旦我知道我正在处理哪个类/方法,我就知道如何检查类。
我考虑过只拆分字符串,所以您最终会得到“Products”和“GetProducts”,然后我可以查找 ProductsController,并在其中查找名为 GetProducts 的方法。
这可能有效,但似乎应该有更好的解决方案。有什么想法吗?
请您参考如下方法:
您可以使用以下方法获取操作的实际 Controller 实例和属性:
public class Somefilter : ActionFilterAttribute
{
public override void OnActionExecuting(HttpActionContext actionContext)
{
var controller = actionContext.ControllerContext.Controller;
var someFilterattributes = actionContext.ActionDescriptor.GetCustomAttributes<Somefilter>()
var otherAttributes = actionContext.ActionDescriptor.GetCustomAttributes<Other>()
}
}
Other 是操作的其他过滤器。
