2008-09-30 8 views

Odpowiedz

14

Możesz pobrać te dane z ViewContext.RouteData. Poniżej przedstawiamy kilka przykładów, jak uzyskać dostęp (i używać), że informacje:

/// Są one dodawane do moich viewmasterpage, klas ViewPage i bazowych 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); 
} 

/// Niektóre metody przedłużania że Dodałem do klasy 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()); 
    } 
} 
+0

Ostrzeżenie: Ten kod nie działa w ASP.NET MVC 5. – qJake

+0

@qJake Potem jak współdzielenie danych między kontrolerami w MVC5? – Mrunal

+0

@Mrunal To pytanie nie dotyczy udostępniania danych między kontrolerami, ale raczej chodzi o to, jak wyświetlić trasy przypisane kontrolerowi. Wiem, że jest to możliwe z ASP.NET MVC 5 (teraz ASP.NET Core), ale nie znam składni offhand. – qJake

3

Możesz użyć <% = Url.Action (akcja, kontroler, wartości)%>, aby zbudować adres URL z poziomu strony wzorcowej.

Czy robisz to, aby podświetlić zakładkę dla bieżącej strony?

Jeśli tak, możesz użyć ViewContext z widoku i uzyskać potrzebne wartości.

1

Napisałem a helper class, który pozwala mi uzyskać dostęp do parametrów trasy. Dzięki temu pomocnikowi możesz uzyskać kontroler, akcję i wszystkie parametry przekazane do akcji.

18

Ten pracował dla mnie:

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

Zwraca aktualny adres URL jako takie; /Home/About

Być może istnieje prostszy sposób na zwrócenie rzeczywistego ciągu trasy?

22

Zawsze staram się wdrożyć najprostsze rozwiązanie, które spełnia wymagania projektu. Jak powiedział Enstein: "Uczyń rzeczy możliwie najprostszymi, ale nie prostszymi". Spróbuj tego.

<%: Request.Path %> 
+0

Dzięki, łatwe i proste +1 – Deano

Powiązane problemy