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

查看:24
本文介绍了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 helper 对象中:

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 不是从 ControllerContext 继承的,就像旧的 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 ActionContext 继承,这是更通用的术语.

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天全站免登陆