HtmlHelper的"ViewContext.Controller"发生了什么? [英] What happened to HtmlHelper's 'ViewContext.Controller'?

查看:55
本文介绍了HtmlHelper的"ViewContext.Controller"发生了什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我的旧ASP.NET Web应用程序中,我编写了HTML帮助程序,以通过访问 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 string GetControllerString(this HtmlHelper htmlHelper) {
    string controllerString = htmlHelper.ViewContext.Controller.ToString();
    return ".NET Controller: " + controllerString;
}

但是,这似乎不再存在于ASP.NET Core的HTML帮助器对象中:

However, this no longer seems to exist in ASP.NET Core's HTML helper object:

public static string GetControllerString(this IHtmlHelper htmlHelper) {
    string controllerString = htmlHelper.ViewContext.Controller.ToString(); // Doesn't exist!
    return ".NET Core Controller: " + controllerString;
}

ViewContext.Controller 怎么了?现在是否不可能从HTML帮助器对象中获取控制器上下文?

What happened to ViewContext.Controller? Is it now impossible to get hold of the controller context from the HTML helper object?

推荐答案

他们对继承链和术语进行了一些更改.因此,ASPNET Core中的 ViewContext 不会像以前的ASPNET MVC

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.

相反, ViewContext

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

由于这个事实, ViewContext 对象上没有继承的 Controller 属性,但是您可以使用 ActionDescriptor 属性,以获得所需的值.

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

这篇关于HtmlHelper的"ViewContext.Controller"发生了什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆