检索ASP.NET MVC中的当前视图名称?

我有一个部分视图(控制),用于多个视图页面,我需要将当前视图的名称传递回控制器 – 所以如果有validation错误,我可以重新绘制原始视图。

解决方法是(在控制器方法中)

var viewName = "Details"; // or whatever ViewData["viewName"] = viewName; return(View(viewName, customer)); 

然后在部分本身中呈现

 <input type="hidden" name="viewName" value="<%=Html.Encode(ViewData["viewName"])%>" /> 

问题是 – 是否有一些属性或语法我可以直接检索,而不是从控制器设置它? 我已经尝试了显而易见的:

 <input type="hidden" name="viewName" value="<%=Html.Encode(this.Name)%>" /> 

但是这不起作用。 我在这里错过了什么?

谢谢。

那么如果你不介意让你的代码绑定到你正在使用的特定的视图引擎,你可以看看ViewContext.View属性并将其转换为WebFormView

 var viewPath = ((WebFormView)ViewContext.View).ViewPath; 

我相信会在最后得到你的视图名称。

编辑: Haacked绝对是现货; 使事情有点整洁我已经用如下扩展方法包装了逻辑:

 public static class IViewExtensions { public static string GetWebFormViewName(this IView view) { if (view is WebFormView) { string viewUrl = ((WebFormView)view).ViewPath; string viewFileName = viewUrl.Substring(viewUrl.LastIndexOf('/')); string viewFileNameWithoutExtension = Path.GetFileNameWithoutExtension(viewFileName); return (viewFileNameWithoutExtension); } else { throw (new InvalidOperationException("This view is not a WebFormView")); } } } 

这似乎正是我所追求的。

我有同样的问题,这就是我解决它的方法:

 namespace System.Web.Mvc { public static class HtmlHelperExtensions { public static string CurrentViewName(this HtmlHelper html) { return System.IO.Path.GetFileNameWithoutExtension( ((RazorView)html.ViewContext.View).ViewPath ); } } } 

然后在视图中:

 var name = Html.CurrentViewName(); 

或干脆

 @Html.CurrentViewName() 

如果你只是想要的行动名称,那么这将做的伎俩:

 public static string ViewName(this HtmlHelper html) { return html.ViewContext.RouteData.GetRequiredString("action"); } 

如果你想从部分视图中获取文件名,这似乎工作:

 public static class HtmlHelperExtensions { public static string GetViewFileName(this HtmlHelper html, object view) { return @"\\"+ view.GetType().FullName.Replace("ASP._Page_", "").Replace("_cshtml", ".cshtml").Replace("_", @"\\"); } } 

在局部看来,你应该这样做:

 var filename = Html.GetViewFileName(this); 

或这个:

 @Html.GetViewFileName(this) 

请做评论,如果这不是一个好方法 – 任何替代品?

最简单的解决scheme是使用ViewBag。

 public ActionResult Index() { ViewBag.CurrentView = "Index"; return View(); } 

在cshtml页面上

 @{ var viewName = ViewBag.CurrentView; } 

要么,

 ((RazorView)ViewContext.View).ViewPath 

你不应该使用像书呆子晚餐工具一样的validation方法吗?

这样,你实际上不需要做这一切,你可以返回视图。

我最近有同样的问题,我想出了代码片段解决了我的问题。

唯一的缺点是Request.UrlReferrer在某些情况下可能为空。 有点晚了,但似乎为我工作,我涵盖了Request.UrlReferrer的所有基地不为空。

  if (Request.UrlReferrer != null) { var viewUrl = Request.UrlReferrer.ToString(); var actionResultName = viewUrl.Substring(viewUrl.LastIndexOf('/')); var viewNameWithoutExtension = actionResultName.TrimStart('/'); } 

刚刚写了一个关于这个博客thingy

http://www.antix.co.uk/A-Developers-Blog/Targeting-Pages-with-CSS-in-ASP.NET-MVC

  /// <summary> /// <para>Get a string from the route data</para> /// </summary> public static string RouteString( this ViewContext context, string template) { foreach (var value in context.RouteData.Values) { template = template.Replace(string.Format("{{{0}}}", value.Key.ToLower()), value.Value == null ? string.Empty : value.Value.ToString().ToLower()); } return template; } 

用法

 <body class="<%= ViewContext.RouteString("{controller}_{action}") %>"> 

编辑:是的,这不会给你视图名称作为第一个评论状态,它给你的控制器和行动。 但是把它留在这里是很有价值的,要知道它没有。