Retrieve the current view name in ASP.NET MVC?

后端 未结 10 864
谎友^
谎友^ 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('/');
     }
    

提交回复
热议问题