Asp.Net MVC: How do I get virtual url for the current controller/view?

前端 未结 5 2021
谎友^
谎友^ 2020-12-24 07:33

Is it possible to get the route/virtual url associated with a controller action or on a view? I saw that Preview 4 added LinkBuilder.BuildUrlFromExpression helper, but it\'

5条回答
  •  误落风尘
    2020-12-24 08:27

    You can get that data from ViewContext.RouteData. Below are some examples for how to access (and use) that information:

    /// These are added to my viewmasterpage, viewpage, and viewusercontrol base classes:

    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);
    }
    

    /// Some extension methods that I added to the UrlHelper class.

    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);
        }
    
        /// 
        /// Determines if the current request is on the specified controller
        /// 
        /// The helper.
        /// Name of the controller.
        /// 
        ///     true if the current view is on the specified controller; otherwise, false.
        /// 
        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);
        }
    
        /// 
        /// Determines if the current request is on the specified controller
        /// 
        /// The type of the controller.
        /// The helper.
        /// 
        ///     true if the current view is on the specified controller; otherwise, false.
        /// 
        public static bool IsController(this UrlHelper helper) where TController : Controller
        {
            return (typeof(TController) == helper.ViewContext.Controller.GetType());
        }
    }
    

提交回复
热议问题