How to get the current viewName(not action name) in ASP .NET MVC3(beta)

柔情痞子 提交于 2019-12-23 03:12:40

问题


I want to use Internationalization for that i need current viewname(not actionname) so that i can accordingly display specific view for that selected culture.

protected override void OnActionExecuted(ActionExecutedContext filterContext)
    {
          string cultureName = Thread.CurrentThread.CurrentCulture.Name;
        //String ViewNameOnly= do something to get viewName 
         if (string.IsNullOrEmpty(ViewNameOnly))
            ViewNameOnly= filterContext.RouteData.Values["action"] + "." + cultureName;

    }

回答1:


protected override void OnActionExecuted(ActionExecutedContext filterContext)
{
    ViewResultBase view = filterContext.Result as ViewResultBase;
    if (view != null) {
        string viewName = view.ViewName;
    }
}



回答2:


filterContext.Result will contain the result returned from the controller action. This is an ActionResult which is a base class for the various results. If it is a ViewResultBase (or one of its derived types), then the ViewName property will give you what you need.



来源:https://stackoverflow.com/questions/8910219/how-to-get-the-current-viewnamenot-action-name-in-asp-net-mvc3beta

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!