我的HelpController出现错误:


  类型HelpController的构造函数包含带有
  名称“ config”,然后键入未注册的HttpConfiguration。
  请确保HttpConfiguration已注册,或更改
  HelpController的构造函数。


是否有其他解决方案,因为我使用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; }
}

09-17 09:46