我试图让它变得通用 - 比如ViewContext.RouteData.GetRequiredString(“controller”),但问题是此时大多数对象都没有实例化,所以我不知道如何获取控制器名称在这一点上。
Public Class TestMeController
Inherits System.Web.Mvc.Controller
Sub New()
ConfigurationManager.AppSettings("ControllerNameWithSpaces") = Utilities.AddSpaceToControllerNameBeforeEachUpperCase("Here I want to pass a name of the controller")
'I could Easily just hard code "TestMe" or "TestMe" And would get back "Test Me" and that would solve it but that is not the point.
End Sub
Function Index() As ActionResult
Return View()
End Function
End Class
所以问题是:
在控制器的Sub New()构造函数中 - 是否有某种方法可以在此特定时刻获取当前类(controllerName))?
答案 0 :(得分:1)
在构造函数中(在C#中),您可以获取类名:
this.GetType().Name
如果内存在VB.Net中起作用,那将是:
Me.GetType().Name '???
在操作中,您可以从RouteData获取C#中的控制器名称:
RouteData.Values["controller"]