如何在ASP.NET MVC 2.0中的视图外创建HtmlHelper?

rjz*_*zii 4 html-helper asp.net-mvc-2

我们正在将ASP.NET MVC 1.0应用程序升级到2.0版本,而某些代码需要使用需要HtmlHelper呈现的LinkExtensions.虽然我们知道某些代码没有正确地遵循MVC模型并且正在根据需要进行重新编码,但我们需要一些工作来实现应用程序的构建.

这是我们在ASP.NET MVC 1.0下工作的当前语法:

public static HtmlHelper GetHtmlHelper(ControllerContext context)
{
    return new HtmlHelper(new ViewContext(context,
                                          new WebFormView("HtmlHelperView"),
                                          new ViewDataDictionary(),
                                          new TempDataDictionary()),
                          new ViewPage());
}
Run Code Online (Sandbox Code Playgroud)

我们得到的错误如下:

错误1'System.Web.Mvc.ViewContext'不包含带有4个参数的构造函数

Dar*_*rov 5

还有一个额外的参数需要TextWriter:

var viewContext = new ViewContext(
    context,
    new WebFormView("HtmlHelperView"),
    new ViewDataDictionary(),
    new TempDataDictionary(),
    context.HttpContext.Response.Output
);
Run Code Online (Sandbox Code Playgroud)

这里的问题是为什么你需要htmlHelper自己实例化而不是使用视图中提供的那个?