The type String cannot be constructed

后端 未结 5 1445
梦如初夏
梦如初夏 2021-02-18 16:29

I\'m using Web.api and Unity and I am getting the following error when trying to open the default \"help\" area:

[InvalidOperationException: The type String cann         


        
相关标签:
5条回答
  • 2021-02-18 16:52

    I've recently had this error happen on a previously working codebase. The following answer shows what I did to correct it:

    Adding Web API and API documentation to an existing MVC project

    Basically make the constructor with parameters protected rather than public

    0 讨论(0)
  • 2021-02-18 16:59

    I think a better way is to add InjectionConstructor attribute to the default constructor. This attribute forces unity to use the decorated constructor.

    Example:

    public class HelpController : Controller
    {
        private const string ErrorViewName = "Error";
    
        [InjectionConstructor]
        public HelpController()
            : this(GlobalConfiguration.Configuration)
        {
        }
    
    0 讨论(0)
  • 2021-02-18 17:00

    Registering the HttpConfiguration object as an instance in UnityContainer will also help resolve the issue.
    Just need to add to add the below line while registering in UnityContainer.

    public static void RegisterTypes(IUnityContainer container) {
        container.RegisterInstance<HttpConfiguration>(GlobalConfiguration.Configuration);
    }
    

    This will help Unity resolve the config parameter, when it invokes the constructor with the parameter.

    public HelpController(HttpConfiguration config) {
        Configuration = config;
    }
    
    0 讨论(0)
  • 2021-02-18 17:09

    As your code sample, I'm assuming you are on a Controller and not a API Controller (from web api).

    Your api controller has a dependency on the constructor from HttpConfiguration. The container problably does not have this definition for this type and consequently does not know how to solve it and the string on the error message should come from this type as a dependency. I recommend you use the GlobalConfiguration static class and access the Configuration property to get a HttpConfiguration instance. You could abstract it in a property, for sample:

    // include this namespace
    using System.Web.Http;
    
    public class HelpController : Controller
    {
        // remove the constructors...
    
        // property
        protected static HttpConfiguration Configuration
        {
            get { return GlobalConfiguration.Configuration; }
        }
    
        public ActionResult Index()
        {
            return View(this.Configuration.Services.GetApiExplorer().ApiDescriptions);
        }
    
        public ActionResult Api(string apiId)
        {
            if (!String.IsNullOrEmpty(apiId))
            {
                HelpPageApiModel apiModel = this.Configuration.GetHelpPageApiModel(apiId);
                if (apiModel != null)
                {
                    return View(apiModel);
                }
            }
    
            return View("Error");
        }
    }
    

    Now, if you are on a Api Controller, you just can access directly the this.Configuration property that is already on the ApiController (base class for Api controllers) and get a instance of HttpConfiguration.

    0 讨论(0)
  • 2021-02-18 17:10

    The reason this happens is because Unity will by default choose to use the constructor with the most parameters thus bypassing the default constructor.

    Comment out the two constructors that exist in the HelpController template and add a default one that sets the configuration.

        //public HelpController()
        //    : this(GlobalConfiguration.Configuration)
        //{
        //}
    
        //public HelpController(HttpConfiguration config)
        //{
        //    Configuration = config;
        //}
    
        public HelpController()
        {
            Configuration = GlobalConfiguration.Configuration;
        }
    
    0 讨论(0)
提交回复
热议问题