如何在ASP.Net Core MVC应用程序中获取操作和控制器名称? [英] How to get Action and Controller name in ASP.Net Core MVC app?
问题描述
如何在Startup.cs
中的ASP.Net MVC Core RC1
应用程序中获取Action
和Controller
名称?
How to get Action
and Controller
names in ASP.Net MVC Core RC1
Application in Startup.cs
?
我想创建一个中间件并在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..
推荐答案
您需要注入 ActionDescriptor
,其中包含该操作的所有路由值.如果将其强制转换为 ControllerName
和ActionName
.
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
.
这篇关于如何在ASP.Net Core MVC应用程序中获取操作和控制器名称?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!