且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

如何在asp.net MVC 3 中获取当前视图名称?

更新时间:2023-11-26 10:24:04

不知道为什么需要获取当前视图名称,但可以在视图中使用 VirtualPath 属性.通常,了解当前操作或控制器更有用.但无论如何,这是获取当前视图名称的方法:

No idea why you would need to get the current view name but you could use the VirtualPath property inside a view. Normally it's more useful to know the current action or controller. But anyway, here's how to get the current view name:

@VirtualPath

如果您只想获取文件名:

and if you wanted to get only the filename:

@Path.GetFileName(Server.MapPath(VirtualPath))

并且没有扩展名:

@Path.GetFileNameWithoutExtension(Server.MapPath(VirtualPath))