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, ViewContextinherits 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 ActionDescriptorproperty, to get the needed value.

publicstaticstringGetControllerString(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'?"