Skip to content Skip to sidebar Skip to footer

What Happened To HtmlHelper's 'ViewContext.Controller'?

In my old ASP.NET web app, I coded an HTML helper to get access to the context of the controller executing the current view by accessing ViewContext.Controller: public static strin

Solution 1:

They've changed inheritance chain and terminology a bit. So, ViewContext in ASPNET Core doesn't inherit from ControllerContext, like in the older ASPNET MVC framework.

Instead, ViewContext inherits from ActionContext which is more generic term.

Due to that fact, there is no inherited Controller property on the ViewContext object, but rather you can use ActionDescriptor property, to get the needed value.

public static string GetControllerString(this IHtmlHelper htmlHelper) {
    string actionDescriptor = htmlHelper.ViewContext.ActionDescriptor.DisplayName;
    return ".NET Core Controller: " + actionDescriptorName;
}

Post a Comment for "What Happened To HtmlHelper's 'ViewContext.Controller'?"