从剃须刀获取控制器名称 [英] Getting controller name from razor
本文介绍了从剃须刀获取控制器名称的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我似乎很难得到应该容易的东西.从我的角度来看,我想使用Razor获取当前控制器的名称.例如,如果我在这里:
I seem to be having a difficult getting something that should be easy. From within my view, using Razor, I'd like to get the name of the current controller. For example, if I'm here:
http://www.example.com/MyController/Index
如何从Razor表达式中获取控制器名称MyController
:
How can I get the controller name, MyController
from a Razor expression:
@* Obviously this next line doesn't work
@Controller.Name
*@
我是MVC的新手,因此,如果这是一个显而易见的答案,请不要攻击我.
I'm new to MVC, so if this is an obvious answer, don't attack me to bad.
推荐答案
@{
var controllerName = this.ViewContext.RouteData.Values["controller"].ToString();
}
OR
@{
var controllerName = HttpContext.Current.Request.RequestContext.RouteData.Values["controller"].ToString();
}
这篇关于从剃须刀获取控制器名称的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文