我的HelpController
中出现错误:
帮助控制器类型的构造函数包含参数 名称"config"并键入未注册的 HttpConfiguration。 请确保已注册 HttpConfiguration,或更改 帮助控制器的构造函数。
有没有其他方法可以解决此问题,因为我使用Configuration
来获取Configuration.GetModelDescriptionGenerator();
public class HelpController : Controller
{
private const string ErrorViewName = "Error";
public HelpController(HttpConfiguration config)
{
Configuration = config;
}
public HttpConfiguration Configuration { get; private set; }
...
public ActionResult ResourceModel(string modelName)
{
if (!String.IsNullOrEmpty(modelName))
{
ModelDescriptionGenerator modelDescriptionGenerator = Configuration.GetModelDescriptionGenerator();
ModelDescription modelDescription;
if (modelDescriptionGenerator.GeneratedModels.TryGetValue(modelName, out modelDescription))
{
return View(modelDescription);
}
}
return View(ErrorViewName);
}
}
将构造函数更改为 private
并放回 VS 生成的无参数构造函数中。您的HelpController
应如下所示。
public class HelpController : Controller
{
private const string ErrorViewName = "Error";
public HelpController()
: this(GlobalConfiguration.Configuration)
{
}
private HelpController(HttpConfiguration config)
{
Configuration = config;
}
public HttpConfiguration Configuration { get; private set; }
}