Asp.Net MVC:如何确定您当前是否在特定视图上
我需要确定我是否在某个特定视图上。 我的用例是我想用当前视图的“on”类来装饰导航元素。 有没有内置的方式这样做?
在这里我正在使用。 我认为这实际上是由VS中的MVC项目模板生成的:
public static bool IsCurrentAction(this HtmlHelper helper, string actionName, string controllerName) { string currentControllerName = (string)helper.ViewContext.RouteData.Values["controller"]; string currentActionName = (string)helper.ViewContext.RouteData.Values["action"]; if (currentControllerName.Equals(controllerName, StringComparison.CurrentCultureIgnoreCase) && currentActionName.Equals(actionName, StringComparison.CurrentCultureIgnoreCase)) return true; return false; }
我目前的解决方案是使用扩展方法:
public static class UrlHelperExtensions { /// /// Determines if the current view equals the specified action /// /// The type of the controller. /// Url Helper /// The action to check. /// /// true if the specified action is the current view; otherwise, false . /// public static bool IsAction(this UrlHelper helper, LambdaExpression action) where TController : Controller { MethodCallExpression call = action.Body as MethodCallExpression; if (call == null) { throw new ArgumentException("Expression must be a method call", "action"); } return (call.Method.Name.Equals(helper.ViewContext.ViewName, StringComparison.OrdinalIgnoreCase) && typeof(TController) == helper.ViewContext.Controller.GetType()); } /// /// Determines if the current view equals the specified action /// /// Url Helper /// Name of the action. /// /// true if the specified action is the current view; otherwise, false . /// public static bool IsAction(this UrlHelper helper, string actionName) { if (String.IsNullOrEmpty(actionName)) { throw new ArgumentException("Please specify the name of the action", "actionName"); } string controllerName = helper.ViewContext.RouteData.GetRequiredString("controller"); return IsAction(helper, actionName, controllerName); } /// /// Determines if the current view equals the specified action /// /// Url Helper /// Name of the action. /// Name of the controller. /// /// true if the specified action is the current view; otherwise, false . /// public static bool IsAction(this UrlHelper helper, string actionName, string controllerName) { if (String.IsNullOrEmpty(actionName)) { throw new ArgumentException("Please specify the name of the action", "actionName"); } if (String.IsNullOrEmpty(controllerName)) { throw new ArgumentException("Please specify the name of the controller", "controllerName"); } if (!controllerName.EndsWith("Controller", StringComparison.OrdinalIgnoreCase)) { controllerName = controllerName + "Controller"; } bool isOnView = helper.ViewContext.ViewName.SafeEquals(actionName, StringComparison.OrdinalIgnoreCase); return isOnView && helper.ViewContext.Controller.GetType().Name.Equals(controllerName, StringComparison.OrdinalIgnoreCase); } }
这里有点不同,使用FilterAttribute:
[NavigationLocationFilter("Products")] public ViewResult List() { return View(); }
…
public class NavigationLocationFilterAttribute : ActionFilterAttribute { public string CurrentLocation { get; set; } public NavigationLocationFilterAttribute(string currentLocation) { CurrentLocation = currentLocation; } public override void OnActionExecuting(ActionExecutingContext filterContext) { var controller = (Controller)filterContext.Controller; controller.ViewData.Add("NavigationLocation", CurrentLocation); } }
…
在视图中:
上述就是C#学习教程:Asp.Net MVC:如何确定您当前是否在特定视图上分享的全部内容,如果对大家有所用处且需要了解更多关于C#学习教程,希望大家多多关注—计算机技术网(www.ctvol.com)!
<%= ViewData["NavigationLocation"] %>
本文来自网络收集,不代表计算机技术网立场,如涉及侵权请联系管理员删除。
ctvol管理联系方式QQ:251552304
本文章地址:https://www.ctvol.com/cdevelopment/1003087.html