C# Net MVC:如何获取当前控制器/视图的虚拟url?
是否可以获取与控制器操作或视图关联的路由/虚拟url?我看到Preview 4添加了LinkBuilder.BuildUrlFromExpression helper,但是如果您想在主控上使用它,它不是很有用,因为控制器类型可以不同。非常感谢您的任何想法。您可以使用在母版页中构建URL 您这样做是为了突出显示当前页面的选项卡还是什么 如果是这样,您可以从视图中使用ViewContext并获得所需的值。我编写了一个允许访问路由参数的程序。使用此帮助程序,您可以获取控制器、操作以及传递给操作的所有参数。这对我来说很有用:C# Net MVC:如何获取当前控制器/视图的虚拟url?,c#,asp.net-mvc,routing,C#,Asp.net Mvc,Routing,是否可以获取与控制器操作或视图关联的路由/虚拟url?我看到Preview 4添加了LinkBuilder.BuildUrlFromExpression helper,但是如果您想在主控上使用它,它不是很有用,因为控制器类型可以不同。非常感谢您的任何想法。您可以使用在母版页中构建URL 您这样做是为了突出显示当前页面的选项卡还是什么 如果是这样,您可以从视图中使用ViewContext并获得所需的值。我编写了一个允许访问路由参数的程序。使用此帮助程序,您可以获取控制器、操作以及传递给操作的所有参
它返回当前的Url<代码>/Home/About
也许有一种更简单的方法可以返回实际的路由字符串?您可以从ViewContext.RoutedData获取该数据。以下是如何访问(和使用)该信息的一些示例: ///这些将添加到my 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());
}
}
公共静态类UrlHelperExtensions
{
///
///确定当前视图是否等于指定的操作
///
///控制器的类型。
///Url助手
///要检查的操作。
///
///如果指定的操作是当前视图,则为true;否则为false。
///
公共静态bool IsAction(此UrlHelper helper,LambdaExpression操作),其中TController:Controller
{
MethodCallExpression调用=action.Body作为MethodCallExpression;
if(call==null)
{
抛出新ArgumentException(“表达式必须是方法调用”、“操作”);
}
return(call.Method.Name.Equals(helper.ViewContext.ViewName,StringComparison.OrdinalIgnoreCase)&&
typeof(TController)=helper.ViewContext.Controller.GetType();
}
///
///确定当前视图是否等于指定的操作
///
///Url助手
///操作的名称。
///
///如果指定的操作是当前视图,则为true;否则为false。
///
公共静态bool IsAction(此UrlHelper,字符串actionName)
{
if(String.IsNullOrEmpty(actionName))
{
抛出新ArgumentException(“请指定操作的名称”,“actionName”);
}
string controllerName=helper.ViewContext.RoutedData.GetRequiredString(“控制器”);
返回IsAction(助手、actionName、controllerName);
}
///
///确定当前视图是否等于指定的操作
///
///Url助手
///操作的名称。
///控制器的名称。
///
///如果指定的操作是当前视图,则为true;否则为false。
///
公共静态bool IsAction(此UrlHelper helper、字符串actionName、字符串controllerName)
{
if(String.IsNullOrEmpty(actionName))
{
抛出新ArgumentException(“请指定操作的名称”,“actionName”);
}
if(String.IsNullOrEmpty(controllerName))
{
抛出新ArgumentException(“请指定控制器名称”、“控制器名称”);
}
if(!controllerName.EndsWith(“Controller”,StringComparison.OrdinalIgnoreCase))
{
控制器名称=控制器名称+“控制器”;
}
bool isOnView=helper.ViewContext.ViewName.SafeEquals(actionName、StringComparison.OrdinalIgnoreCase);
返回isOnView&&helper.ViewContext.Controller.GetType().Name.Equals(controllerName、StringComparison.OrdinalIgnoreCase);
}
///
///确定当前请求是否在指定的控制器上
///
///助手。
///控制器的名称。
///
///如果当前视图位于指定的控制器上,则为true;否则为false。
///
公共静态bool IsController(此UrlHelper,字符串controllerName)
{
if(String.IsNullOrEmpty(controllerName))
{
抛出新ArgumentException(“请指定控制器名称”、“控制器名称”);
}
if(!controllerName.EndsWith(“Controller”,StringComparison.OrdinalIgnoreCase))
{
控制器名称=控制器名称+“控制器”;
}
返回helper.ViewContext.Controller.GetType().Name.Equals(controllerName、StringComparison.OrdinalIgnoreCase);
}
///
///确定当前请求是否在指定的控制器上
///
///控制器的类型。
///助手。
///
///如果当前视图位于指定的控制器上,则为true;否则为false。
///
公共静态bool IsController(此UrlHelper helper),其中TController:Controller
{
return(typeof(TController)=helper.ViewContext.Controller.GetType());
}
}
我总是尝试实施满足项目要求的最简单解决方案。正如恩斯坦所说,“让事情尽可能简单,但不要简单。”试试这个
<%: Request.Path %>
警告:此代码在ASP.NET MVC 5中不再有效。@qJake那么如何在MVC5中的控制器之间共享数据?@Mrunal此问题不是关于在控制器之间共享数据,而是关于如何查看带有控制器的路由。我知道用ASP.NETMVC5(现在是ASP.NETCore)是可能的,但我现在还不知道语法。@qJake:谢谢。实际上,我用MVC5启动了一个ASP.NETCore项目。在这种情况下,我尝试使用HttpContext.Current.Session,但在我想要设置变量的实用程序类中无法访问相同的内容。因为我想,一旦我得到了,我可以通过会话在控制器之间传递数据。