The type String cannot be constructed

后端 未结 5 1447
梦如初夏
梦如初夏 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条回答
  •  梦毁少年i
    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.

提交回复
热议问题