【问题标题】:HtmlHelper extension to find view name用于查找视图名称的 HtmlHelper 扩展
【发布时间】:2012-06-12 21:00:45
【问题描述】:

我正在尝试创建一个HtmlExtension 来检索当前视图的名称。

但是,我不想看到请求的视图(例如“/Account/LogOn”的“LogOn”),而是正在处理的实际文件(例如"_Layout")。

我能找到的最接近的是html.ViewDataContainer.ToString(),例如它返回{ASP._Page_Views_Shared__Layout_cshtml},但我认为解析它不是一个好主意。

这些信息是否在 Html 扩展中可用?

提前致谢。

【问题讨论】:

标签: c# asp.net asp.net-mvc-3 html-helper


【解决方案1】:

你可以像这样创建一个扩展,

public static string ViewName(this WebViewPage page)
{
  return Path.GetFileNameWithoutExtension(page.VirtualPath);
}

然后从剃刀的角度来看,

The view name is: @this.ViewName()

或不带扩展名,

The view name is: @Path.GetFileNameWithoutExtension(VirtualPath)

【讨论】:

    猜你喜欢
    • 2011-05-07
    • 2010-10-27
    • 2015-04-04
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-06-10
    • 1970-01-01
    相关资源
    最近更新 更多