5

How to get Action and Controller names in ASP.Net MVC Core RC1 Application in Startup.cs?

I want to create a middleware and log the information (I want to Log detailed response to my Database, so I need Action and Controller info.) after following code in configure method of startup.cs -

 app.UseMvc(routes =>
{
     routes.MapRoute(
            name: "default",
            template: "{controller=User}/{action=Index}/{id?}");
 });

//Want to get Action and controller names here..
3
  • I think, you should override the route middleware to do that, you can't add a middleware after it, it going to not be called. Commented Apr 19, 2016 at 19:33
  • @aguafrommars, can you elaborate your point? Commented Apr 19, 2016 at 19:39
  • After reading the code posted by @Clint B, you can use his middleware, it does what you wanna do. You must use it before UseMcv in the stack. It works on response. Commented Apr 19, 2016 at 19:51

2 Answers 2

8

You'll want to inject the IActionDescriptorCollectionProvider service into your middleware. From that, you can use the property ActionDescriptors.Items to get a list of ActionDescriptor, which has all the route values for that action. If you cast that to ControllerActionDescriptor, you'll have access to ControllerName and ActionName.

Sign up to request clarification or add additional context in comments.

1 Comment

This will give you all the controllers and their actions. But as stated in problem scenario, he need the specific controller and action name which is currently triggered to log their information
1

This is how I do it in my custom exception handler middleware.

using System.Diagnostics;

frame = new StackTrace(e, true).GetFrame(0);
controller = frame.GetMethod().DeclaringType.FullName;
action = frame.GetMethod().ToString();

If you'd like to checkout the middleware project, here's the link CustomExceptionHandler

EDIT:
You could also do your logging in an action filter. The OnActionExecuting() method of an action filter has an ActionExecutingContext parameter. With that parameter you can get all kinds of info about the request. Below is how you would get the controller and action name. And I would suggest doing it in a separate thread to help with responsiveness.

public override void OnActionExecuting(ActionExecutingContext context)
{
    var t = Task.Run(() => {
        controller = context.Controller.ToString();
        action = context.ActionDescriptor.Name;

        //Log to DB
    }
}

5 Comments

I am exploring this option, the only problem i am facing is something else. My Middleware code is not firing for WEB API calls. Do you have any idea?
Are you saying the Invoke() method of your middleware is not being called? That method is called on every request.
The invoke method is only being called for certain URL's, but not for all, especially they are not being fired for Web Api end points. I will create brand new solution and will give it a try. I will let you know my findings.
That would mean there is a bug in the .NET framework. Which I don't think is the case. But for what you're doing maybe using a filter would be better. I'll edit my answer to add that.
That's late, but be sure to call app.UseMiddleware() before app.UseMvc()

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.