Retrieve the current view name in ASP.NET MVC?

后端 未结 10 861
谎友^
谎友^ 2020-11-28 09:37

I have a partial view (control) that\'s used across several view pages, and I need to pass the name of the current view back to the controller - so if there\'s e.g. validati

相关标签:
10条回答
  • 2020-11-28 10:10

    I had the same issue recently and the snippet of code I came up with solved my issue.

    The only downfall is that Request.UrlReferrer could be null in some cases. Bit late but seemed to work for me and I covered all the bases of Request.UrlReferrer not being null.

     if (Request.UrlReferrer != null)
     {
          var viewUrl = Request.UrlReferrer.ToString();
          var actionResultName = viewUrl.Substring(viewUrl.LastIndexOf('/'));
          var viewNameWithoutExtension = actionResultName.TrimStart('/');
     }
    
    0 讨论(0)
  • 2020-11-28 10:11

    Well if you don't mind having your code tied to the specific view engine you're using, you can look at the ViewContext.View property and cast it to WebFormView

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

    I believe that will get you the view name at the end.

    EDIT: Haacked is absolutely spot-on; to make things a bit neater I've wrapped the logic up in an extension method like so:

    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"));
            }
        }
    }
    

    which seems to do exactly what I was after.

    0 讨论(0)
  • 2020-11-28 10:13

    Easiest solution is using ViewBag.

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

    On the cshtml page

    @{
    var viewName = ViewBag.CurrentView;
    }
    

    Or,

    ((RazorView)ViewContext.View).ViewPath
    
    0 讨论(0)
  • 2020-11-28 10:18

    You can use Razor:

    in your View header

    @{
        ViewData["Title"] = "YourViewName";    
    }
    

    in your View HTML

    @{
       var _nameCurrentView = ViewContext.ViewData["Title"];
    }
    

    use in your html the variable @_nameCurrentView

      <li class="breadcrumb-item active">@_nameCurrentView</li>
    

    or use in your action

      ViewData["Title"]
    
    0 讨论(0)
  • 2020-11-28 10:18

    Just wrote a blog thingy about this

    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;
      }
    

    usage

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

    EDIT : Yes this is not going to give you the view name as the first comment states, it gives you the controller and action. But leaving it here as its valuable to know that it doesn't.

    0 讨论(0)
  • 2020-11-28 10:24

    If you are looking for solution for asp.net core you can use:

    @System.IO.Path.GetFileNameWithoutExtension(ViewContext.View.Path)
    

    This will return the current view name.

    0 讨论(0)
提交回复
热议问题