从剃刀获取控制器名称 [英] Getting controller name from razor

查看:21
本文介绍了从剃刀获取控制器名称的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我似乎很难获得应该很容易的东西.在我看来,使用 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();
}

@{ 
    var controllerName = HttpContext.Current.Request.RequestContext.RouteData.Values["controller"].ToString();
}

这篇关于从剃刀获取控制器名称的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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