コントローラのアクションまたはビューに関連付けられたルート/仮想URLを取得することはできますか?プレビュー4はLinkBuilder.BuildUrlFromExpressionヘルパーを追加しましたが、コントローラーのタイプが異なる可能性があるため、マスターで使用する場合はそれほど有用ではありません。どのような考えが評価されます。Asp.Net MVC:現在のコントローラ/ビューの仮想URLを取得するにはどうすればよいですか?
答えて
このデータは、ViewContext.RouteDataから取得できます。以下は、アクセス(および使用)する方法についていくつかの例、その情報は、以下のとおりです。
///これらは私のviewmasterpage、ビューページ、および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());
}
}
あなたは、マスターページ内からURLを構築するために<% = Url.Action(アクション、コントローラ、値)%>を使用することができます。
現在のページなどのタブをハイライト表示していますか?
ビューからViewContextを使用して、必要な値を取得できます。
私はa helper classを書いて、私にルートパラメータにアクセスすることができます。このヘルパーを使用すると、アクションに渡されたコントローラ、アクション、およびすべてのパラメータを取得できます。
これが私の仕事:
<%= this.Url.RouteUrl(this.ViewContext.RouteData.Values) %>
をそれのような現在のURLを返します。 /Home/About
おそらく実際のルート文字列を返す簡単な方法がありますか?
私は常にプロジェクトの要件を満たす最も簡単なソリューションを実装しようとします。 Ensteinが言ったように、「できるだけシンプルではなく、できるだけシンプルにする」これを試して。
<%: Request.Path %>
ありがとう、簡単で簡単な+1 – Deano
- 1. MVCアプリケーションで現在のユーザーを取得するにはどうすればよいですか?
- 2. TWIGで現在のURLまたはルートを取得するにはどうすればよいですか?
- 3. 現在のリクエストのurl(window.location)をAngular 5で取得するにはどうすればよいですか?
- 4. DotNetNukeでポータルの現在のログインURLを取得するにはどうすればよいですか?
- 5. Yiiでparamsなしで現在のURLを取得するにはどうすればよいですか?
- 6. ASP.Net Webサイトから現在のイメージ名を取得するにはどうすればよいですか?
- 7. FilterAttribute内から現在のURLを取得するにはどうすればよいですか?
- 8. ブラウザから現在のタブURLを取得するにはどうすればよいですか?
- 9. ASP.NET MVC:現在のリクエストの実際のURLを確実に取得するにはどうすればよいですか?
- 10. asp.net mvcの現在のルートからURLパラメータを取得するには?
- 11. ビュー内の現在のページURLを取得するにはどうすればよいですか?
- 12. 現在のクロムタブの親タブURLを取得するにはどうすればよいですか?
- 13. appengineアプリの現在のサーバーURLを取得するにはどうすればよいですか?
- 14. リフトスニペットで現在のページURLを取得するにはどうすればよいですか?
- 15. Selenium Webdriver 2 Pythonで現在のURLを取得するにはどうすればよいですか?
- 16. Yii 1.xで現在のURLを取得するにはどうすればよいですか?
- 17. 現在のベースURLを動的に取得するにはどうすればよいですか?
- 18. ASP.NET MVCアクションでリファラーURLを取得するにはどうすればよいですか?
- 19. QtWebKitベースのブラウザ用の仮想キーボード、または現在フォーカスのあるテキストフィールドを取得するにはどうすればよいですか?
- 20. ASP.NET MVC:ビュー内のページの現在のURLをどのように取得しますか?
- 21. c#asp.net - 別のスレッドで現在のページURLを検索するにはどうすればよいですか?
- 22. 現在ログインしているユーザーのIDを取得するにはどうすればよいですか?
- 23. ASP.NET MVCカスタムURL - 仮想サーバーのパス
- 24. 仮想プライベートサーバからテキストファイルを取得するにはどうすればよいですか?
- 25. .Net Coreミドルウェア内で現在のサブドメインを取得するにはどうすればよいですか?
- 26. asp.net webformsまたはasp.net mvcでこのURLパターンをリダイレクトするにはどうすればよいですか?
- 27. Pythonで現在のファイル、現在のクラス、および現在のメソッドを取得するにはどうすればよいですか?ファイルの
- 28. SeriLogシンクで現在のHttpContextを取得するにはどうすればよいですか?
- 29. ASP.NET MVC現在のホストを取得
- 30. ASP.net MVC現在のビューエンジンを取得
警告:このコードは、もはやMVC5のコントローラ間でデータを共有する方法を次にASP.NET MVC 5 – qJake
@qJakeで動作しますか? – Mrunal
@Mrunalこの質問は、コントローラ間でデータを共有することではなく、コントローラで連携されたルートを表示する方法に関するものです。 ASP.NET MVC 5(現時点ではASP.NET Core)で可能であることは分かっていますが、構文はわかりません。 – qJake