Asp.Net MVC:So ermitteln Sie, ob Sie sich derzeit in einer bestimmten Ansicht befinden

StackOverflow https://stackoverflow.com/questions/40535

  •  09-06-2019
  •  | 
  •  

Frage

Ich muss feststellen, ob ich eine bestimmte Ansicht vertrete.Mein Anwendungsfall ist, dass ich Navigationselemente mit einer „on“-Klasse für die aktuelle Ansicht dekorieren möchte.Gibt es eine eingebaute Möglichkeit, dies zu tun?

War es hilfreich?

Lösung

Hier, was ich verwende.Ich denke, dass dies tatsächlich von der MVC-Projektvorlage in VS generiert wird:

public static bool IsCurrentAction(this HtmlHelper helper, string actionName, string controllerName)
    {
        string currentControllerName = (string)helper.ViewContext.RouteData.Values["controller"];
        string currentActionName = (string)helper.ViewContext.RouteData.Values["action"];

        if (currentControllerName.Equals(controllerName, StringComparison.CurrentCultureIgnoreCase) && currentActionName.Equals(actionName, StringComparison.CurrentCultureIgnoreCase))
            return true;

        return false;
    }

Andere Tipps

Meine aktuelle Lösung ist mit Erweiterungsmethoden:

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);
    }
}

Hier ist etwas anderes: Verwenden Sie ein FilterAttribute:

    [NavigationLocationFilter("Products")]
    public ViewResult List()
    {
        return View();
    }

...

public class NavigationLocationFilterAttribute : ActionFilterAttribute
{
    public string CurrentLocation { get; set; }

    public NavigationLocationFilterAttribute(string currentLocation)
    {
        CurrentLocation = currentLocation;
    }

    public override void OnActionExecuting(ActionExecutingContext filterContext)
    {
        var controller = (Controller)filterContext.Controller;
        controller.ViewData.Add("NavigationLocation", CurrentLocation);
    }
}

...

Und in der Ansicht:

<%= ViewData["NavigationLocation"] %>
Lizenziert unter: CC-BY-SA mit Zuschreibung
Nicht verbunden mit StackOverflow
scroll top