是否有可能獲得與控制器操作或視圖關聯的路由/虛擬url?我看到Preview 4添加了LinkBuilder.BuildUrlFromExpression助手,但是如果您想在主控上使用它,它並不是非常有用,因爲控制器類型可能不同。任何想法都表示讚賞。Asp.Net MVC:我如何獲得當前控制器/視圖的虛擬url?
18
A
回答
14
您可以從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());
}
}
3
您可以使用<%= Url.Action(動作,控制器,值)%>從母版頁中構建URL。
你這樣做可能會突出顯示當前頁面的標籤或什麼?
如果是這樣,您可以從視圖中使用ViewContext並獲取所需的值。
1
我寫了a helper class,它允許我訪問路由參數。藉助這個幫助程序,您可以獲取傳遞給操作的控制器,操作和所有參數。
18
這爲我工作:
<%= this.Url.RouteUrl(this.ViewContext.RouteData.Values) %>
它返回當前URL這樣; /Home/About
也許有一個更簡單的方法來返回實際的路由字符串?
22
我總是嘗試實施符合項目要求的最簡單的解決方案。正如恩斯坦所說:「讓事情儘可能簡單,但並不簡單。」嘗試這個。
<%: Request.Path %>
相關問題
- 1. ASP.NET MVC:如何在視圖中獲取頁面的當前URL?
- 2. 如何獲取推送我當前視圖的先前視圖控制器
- 3. 如何從URL獲得ID在asp.net MVC控制器
- 4. 如何在頁面視圖控制器中獲取當前視圖控制器
- 5. ASP.NET MVC認爲我的虛擬目錄是一個控制器
- 6. 如何從其他視圖控制器獲取當前視圖?
- 7. iOS - 獲取當前視圖控制器
- 8. 如何獲得當前控制器和動作控制器
- 9. 在ASP.NET MVC中使用HtmlHelper獲取當前視圖的Url 3
- 10. ASP.NET MVC - 在視圖或控制器中獲取當前區域名稱
- 11. ASP.net MVC獲取當前視圖引擎
- 12. asp.net mvc虛擬視圖和web配置
- 13. 在當前視圖控制器下加載視圖控制器
- 14. 如何從URL(ASP.NET MVC)中獲取視圖中的當前路由ID
- 15. 控制器中的方法來獲得當前的url
- 16. 獲取當前視圖控制器名稱在父視圖控制器
- 17. 我應該讓我的ASP.NET MVC控制器操作爲虛擬嗎?
- 18. 如何獲得當前視圖頁面的PHP MVC
- 19. ASP.Net MVC隨機,控制器和視圖
- 20. 部分視圖控制器ASP.NET MVC
- 21. 重用ASP.NET MVC視圖和控制器
- 22. ASP.NET MVC 2.0:共享視圖控制器
- 23. ASP.Net MVC控制器和視圖交互
- 24. asp.net mvc - 視圖和控制器
- 25. 如何在當前視圖控制器視圖上添加UIview?
- 26. ASP.Net MVC 3:多個視圖/ URL到一個控制器動作
- 27. MVC ASP.NET當我提交給控制器時,在我的視圖中找回ViewData.Model
- 28. ASP.NET-MVC3:如何獲得一個動作的URL在控制器
- 29. ASP.NET MVC - 如何從局部視圖中獲取當前操作?
- 30. 虛擬視圖與Spring MVC
警告:此代碼不再在ASP.NET MVC 5 – qJake 2015-03-28 16:42:18
@qJake工作,那麼如何才能MVC5控制器之間的數據共享? – Mrunal 2016-07-06 09:04:08