C# Asp.Net MVC:如何获取当前控制器/视图的虚拟 url?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/151963/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Asp.Net MVC: How do I get virtual url for the current controller/view?
提问by Jim Geurts
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's not very useful if you want to use it on the master, since the controller type can be different. Any thoughts are appreciated.
是否可以获取与控制器操作或视图关联的路由/虚拟 url?我看到 Preview 4 添加了 LinkBuilder.BuildUrlFromExpression helper,但是如果你想在 master 上使用它不是很有用,因为控制器类型可以不同。任何想法表示赞赏。
采纳答案by Jim Geurts
You can get that data from ViewContext.RouteData. Below are some examples for how to access (and use) that information:
您可以从 ViewContext.RouteData 获取该数据。以下是有关如何访问(和使用)该信息的一些示例:
/// These are added to my viewmasterpage, viewpage, and viewusercontrol base classes:
/// 这些被添加到我的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);
}
/// Some extension methods that I added to the UrlHelper class.
/// 我添加到 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());
}
}
回答by Ben Scheirman
You can use <%= Url.Action(action, controller, values) %> to build the URL from within the master page.
您可以使用 <%= Url.Action(action, controller, values) %> 从母版页中构建 URL。
Are you doing this to maybe highlight a tab for the current page or something?
您这样做是为了突出显示当前页面的标签还是什么?
If so you can use ViewContext from the view and get the values you need.
如果是这样,您可以从视图中使用 ViewContext 并获取所需的值。
回答by Jim Geurts
I wrote a helper classthat allows me to access the route parameters. With this helper, you can get the controller, action, and all parameters passed to the action.
我写了一个帮助类,允许我访问路由参数。使用这个助手,你可以获得控制器、动作和传递给动作的所有参数。
回答by Jim Geurts
This worked for me:
这对我有用:
<%= this.Url.RouteUrl(this.ViewContext.RouteData.Values) %>
<%= this.Url.RouteUrl(this.ViewContext.RouteData.Values) %>
It returns the current Url as such; /Home/About
它返回当前的 URL; /Home/About
Maybe there is a simpler way to return the actual route string?
也许有更简单的方法来返回实际的路由字符串?
回答by Tarzan
I always try to implement the simplest solution that meets the project requirements. As Enstein said, "Make things as simple as possible, but not simpler." Try this.
我总是尝试实现满足项目要求的最简单的解决方案。正如恩斯坦所说,“让事情尽可能简单,但不要更简单。” 尝试这个。
<%: Request.Path %>