ASP.NET MVC:如何输出当前正在呈现的Controller和View?

goo*_*ate 3 asp.net-mvc routes asp.net-mvc-3

我有一套复杂的路线,我需要编辑一个特定的网页.给定URL,如何确定创建该页面的控制器和视图?

我愿意使用ASP.NET MVC将信息直接写入textcolor == background color或您可能推荐的任何其他内容的页面.

我想要一个可以在生产中使用的解决方案(禁用MVC路由调试器)

Den*_*aub 6

您可以直接通过访问控制器和操作ViewContext.

// ASP.Net MVC 3
ViewContext.Controller.ValueProvider.GetValue("controller").RawValue
ViewContext.Controller.ValueProvider.GetValue("action").RawValue

// ASP.Net MVC 2 and below:
ViewContext.Controller.ValueProvider["controller"].RawValue
ViewContext.Controller.ValueProvider["action"].RawValue
Run Code Online (Sandbox Code Playgroud)

要获得观点,请看看Phil Haack对类似问题的答案.

  • 或者:ViewContext.RouteData.Values ["controller"]和ViewContext.RouteData.Values ["action"] (2认同)