How can i get the MethodInfo of the controller action that will get called given a request?

孤街浪徒 提交于 2019-12-08 19:15:35

问题


I have a controller and action that's responsible for handling 403s due to the users not being in the correct roles. It has access to the original RequestContext that caused the exception.

What I would like to be able to do is decorate my actions with a description of what they do, then allow the user to notify their manager, requesting access including the description in an email.

So, how can I work out what action would be called given a RequestContext?

Obviously this is more complicated that getting controller and action names out of the RouteData since there are often overloads of an action method etc.

Once I have the MethodInfo then it's easy to get attributes etc.


回答1:


Trying to work it out retrospectively is a bit of a can of worms, as you'd probably need to use reflection to discover the correct method - It would probably be simpler to insert the required data into the HttpContext.Items as part of the code where the authorization fails? It would then be available from your handling method through RequestContext.HttpContext.Items.




回答2:


Here's an extension method for you. If you're doing dependency injection on your controllers (non-parameterless constructor), you'll need to enumerate the controller constructors with reflection, or use your IOC container to instantiate your controller, rather than using Activator.CreateInstance. Also, this can be modified to work with similar context's like ExceptionContext or HttpContext pretty easily.

public static class RequestContextExtensions
{
    public static MethodInfo GetActionMethod(this RequestContext requestContext)
    {
        Type controllerType = Assembly.GetExecutingAssembly().GetTypes().FirstOrDefault(x => x.Name == requestContext.RouteData.Values["controller"].ToString());
        ControllerContext controllerContext = new ControllerContext(requestContext, Activator.CreateInstance(controllerType) as ControllerBase);
        ControllerDescriptor controllerDescriptor = new ReflectedControllerDescriptor(controllerType);
        ActionDescriptor actionDescriptor = controllerDescriptor.FindAction(controllerContext, controllerContext.RouteData.Values["action"].ToString());
        return (actionDescriptor as ReflectedActionDescriptor).MethodInfo;
    }
}



回答3:


I have answered my own question, that is very similar to this.

  • question: How do I get the MethodInfo of an action, given action, controller and area names?
  • answer: https://stackoverflow.com/a/13044838/195417

If this is still of your interest, maybe I can go deeper into it.

My question didn't start from an URL as yours, but from controller and action names, and also the http method (GET, POST...).



来源:https://stackoverflow.com/questions/10814147/how-can-i-get-the-methodinfo-of-the-controller-action-that-will-get-called-given

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!