gpt4 book ai didi

c# - HtmlHelper 的 'ViewContext.Controller' 发生了什么?

转载 作者:行者123 更新时间:2023-11-30 22:52:55 37 4
gpt4 key购买 nike

在我的旧 ASP.NET 网络应用程序中,我编写了一个 HTML 帮助程序,以通过访问 ViewContext.Controller 来访问执行当前 View 的 Controller 的上下文:

public static string GetControllerString(this HtmlHelper htmlHelper) {
string controllerString = htmlHelper.ViewContext.Controller.ToString();
return ".NET Controller: " + controllerString;
}

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

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

ViewContext.Controller 发生了什么?现在不可能从 HTML 帮助器对象获取 Controller 上下文吗?

最佳答案

他们稍微改变了继承链和术语。因此,ASPNET Core 中的 ViewContext 不像旧的 ASPNET MVC framework 那样继承自 ControllerContext .

相反,ViewContext inherits来自 ActionContext,这是一个更通用的术语。

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

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

关于c# - HtmlHelper 的 'ViewContext.Controller' 发生了什么?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57725757/

37 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com