如何在ASP.Net Core MVC应用程序中获取操作和控制器名称? [英] How to get Action and Controller name in ASP.Net Core MVC app?

查看:706
本文介绍了如何在ASP.Net Core MVC应用程序中获取操作和控制器名称?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何在Startup.cs中的ASP.Net MVC Core RC1应用程序中获取ActionController名称?

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..

推荐答案

您需要注入 ,您将可以访问ControllerNameActionName.

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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆