Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get Action and Controller name in ASP.Net Core MVC app?

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..
like image 794
ramiramilu Avatar asked Mar 13 '23 13:03

ramiramilu


1 Answers

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.

like image 196
saluce Avatar answered Mar 25 '23 06:03

saluce