Asp.Net MVC:如何获取当前控制器/视图的虚拟URL?
时间:2020-03-06 14:54:34 来源:igfitidea点击:
是否可以获取与控制器动作或者视图关联的路由/虚拟URL?我看到Preview 4添加了LinkBuilder.BuildUrlFromExpression帮助器,但是如果要在主服务器上使用它并不是很有用,因为控制器类型可以不同。任何想法表示赞赏。
解决方案
我们可以使用<%= Url.Action(action,controller,values)%>从母版页中构建URL。
我们是否正在执行此操作以突出显示当前页面的标签或者其他内容?
如果是这样,我们可以从视图中使用ViewContext并获取所需的值。
我编写了一个帮助程序类,该类允许我访问路由参数。使用此帮助程序,我们可以获取控制器,操作以及传递给操作的所有参数。
这为我工作:
<%= this.Url.RouteUrl(this.ViewContext.RouteData.Values)%>
它照原样返回当前的网址; / Home /关于
也许有一种更简单的方法来返回实际的路线字符串?
我们可以从ViewContext.RouteData获取该数据。以下是一些有关如何访问(和使用)该信息的示例:
///这些被添加到我的viewmasterpage,viewpage和viewusercontrol基类中:
public bool IsController(string controller) { if (ViewContext.RouteData.Values["controller"] != null) { return ViewContext.RouteData.Values["controller"].ToString().Equals(controller, StringComparison.OrdinalIgnoreCase); } return false; } public bool IsAction(string action) { if (ViewContext.RouteData.Values["action"] != null) { return ViewContext.RouteData.Values["action"].ToString().Equals(action, StringComparison.OrdinalIgnoreCase); } return false; } public bool IsAction(string action, string controller) { return IsController(controller) && IsAction(action); }
///我添加到UrlHelper类的一些扩展方法。
public static class UrlHelperExtensions { /// <summary> /// Determines if the current view equals the specified action /// </summary> /// <typeparam name="TController">The type of the controller.</typeparam> /// <param name="helper">Url Helper</param> /// <param name="action">The action to check.</param> /// <returns> /// <c>true</c> if the specified action is the current view; otherwise, <c>false</c>. /// </returns> public static bool IsAction<TController>(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()); } /// <summary> /// Determines if the current view equals the specified action /// </summary> /// <param name="helper">Url Helper</param> /// <param name="actionName">Name of the action.</param> /// <returns> /// <c>true</c> if the specified action is the current view; otherwise, <c>false</c>. /// </returns> 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); } /// <summary> /// Determines if the current view equals the specified action /// </summary> /// <param name="helper">Url Helper</param> /// <param name="actionName">Name of the action.</param> /// <param name="controllerName">Name of the controller.</param> /// <returns> /// <c>true</c> if the specified action is the current view; otherwise, <c>false</c>. /// </returns> 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); } /// <summary> /// Determines if the current request is on the specified controller /// </summary> /// <param name="helper">The helper.</param> /// <param name="controllerName">Name of the controller.</param> /// <returns> /// <c>true</c> if the current view is on the specified controller; otherwise, <c>false</c>. /// </returns> public static bool IsController(this UrlHelper helper, string controllerName) { if (String.IsNullOrEmpty(controllerName)) { throw new ArgumentException("Please specify the name of the controller", "controllerName"); } if (!controllerName.EndsWith("Controller", StringComparison.OrdinalIgnoreCase)) { controllerName = controllerName + "Controller"; } return helper.ViewContext.Controller.GetType().Name.Equals(controllerName, StringComparison.OrdinalIgnoreCase); } /// <summary> /// Determines if the current request is on the specified controller /// </summary> /// <typeparam name="TController">The type of the controller.</typeparam> /// <param name="helper">The helper.</param> /// <returns> /// <c>true</c> if the current view is on the specified controller; otherwise, <c>false</c>. /// </returns> public static bool IsController<TController>(this UrlHelper helper) where TController : Controller { return (typeof(TController) == helper.ViewContext.Controller.GetType()); } }