【问题标题】:Asp.Net MVC: How do I get virtual url for the current controller/view?Asp.Net MVC:如何获取当前控制器/视图的虚拟 url?
【发布时间】:2010-09-14 04:43:39
【问题描述】:

是否可以获取与控制器操作或视图关联的路由/虚拟 url?我看到 Preview 4 添加了 LinkBuilder.BuildUrlFromExpression 助手,但如果你想在 master 上使用它并不是很有用,因为控制器类型可以不同。任何想法表示赞赏。

【问题讨论】:

    标签: c# asp.net-mvc routing


    【解决方案1】:

    这对我有用:

    <%= this.Url.RouteUrl(this.ViewContext.RouteData.Values) %>

    它返回当前的 Url; /Home/About

    也许有更简单的方法来返回实际的路由字符串?

    【讨论】:

      【解决方案2】:

      我总是尝试实施满足项目要求的最简单的解决方案。正如恩斯坦所说,“让事情尽可能简单,但不要简单。”尝试这个。

      <%: Request.Path %>
      

      【讨论】:

        【解决方案3】:

        您可以从 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());
            }
        }
        

        【讨论】:

        • 警告:此代码不再适用于 ASP.NET MVC 5。
        • @qJake 那么如何在MVC5中的控制器之间共享数据呢?
        • @Mrunal 这个问题不是关于在控制器之间共享数据,而是关于如何查看控制器分配的路由。我知道使用 ASP.NET MVC 5(现在是 ASP.NET Core)是可能的,但我不知道语法。
        • @qJake:谢谢。实际上,我使用 MVC 5 使用 ASP.Net Core 启动了一个项目。在那,我尝试使用 HttpContext.Current.Session 但我无法在我想要设置-获取变量的实用程序类中访问相同的项目。因为我猜,一旦我知道了,我就可以通过会话在控制器之间传递数据。
        【解决方案4】:

        我写了a helper class,它允许我访问路由参数。使用此帮助器,您可以获取控制器、操作以及传递给操作的所有参数。

        【讨论】:

          【解决方案5】:

          您可以使用 从母版页中构建 URL。

          您这样做是为了突出显示当前页面的标签或其他内容吗?

          如果是这样,您可以从视图中使用 ViewContext 并获取您需要的值。

          【讨论】:

            猜你喜欢
            • 2011-05-25
            • 1970-01-01
            • 1970-01-01
            • 2010-11-02
            • 2012-12-02
            • 1970-01-01
            • 2014-03-18
            • 1970-01-01
            • 2023-03-21
            相关资源
            最近更新 更多