当前位置:  开发笔记 > 编程语言 > 正文

Asp.Net MVC:如何获取当前控制器/视图的虚拟URL?

如何解决《Asp.NetMVC:如何获取当前控制器/视图的虚拟URL?》经验,为你挑选了3个好方法。

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



1> Tarzan..:

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

<%: Request.Path %>



2> 小智..:

这对我有用:

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

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

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



3> Jim Geurts..:

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

推荐阅读
女女的家_747
这个屌丝很懒,什么也没留下!
DevBox开发工具箱 | 专业的在线开发工具网站    京公网安备 11010802040832号  |  京ICP备19059560号-6
Copyright © 1998 - 2020 DevBox.CN. All Rights Reserved devBox.cn 开发工具箱 版权所有