尝试创建“ XYZController”类型的控制器时发生错误。确保控制器具有无参数的公共构造函数

时间:2019-01-08 11:29:55

标签: c# asp.net-web-api

从Visual Studio执行以下代码时,效果很好,但是当我发布它并在IIS中创建一个网站来访问它时,出现以下错误:

代码:

public class HelloController : ApiController
    {     

        public string HelloMethod()
        {
            return "Hello World";
        }

    }

错误:

An error has occurred.","ExceptionMessage":"An error occurred when trying to create a controller of
type 'HelloController'. Make sure that the controller has a parameterless public
constructor.","ExceptionType":"System.InvalidOperationException","StackTrace":" at
System.Web.Http.Dispatcher.DefaultHttpControllerActivator.Create(HttpRequestMessage request, HttpControllerDescriptor
controllerDescriptor, Type controllerType)\r\n at
System.Web.Http.Dispatcher.HttpControllerDispatcher.d__15.MoveNext()","InnerException":{"Message":"An error has
occurred.","ExceptionMessage":"Retrieving the COM class factory for component with CLSID
{A59E7747-3284-435B-8947-4E004361A6AC} failed due to the following error: 80040154 Class not registered (Exception
from HRESULT: 0x80040154
(REGDB_E_CLASSNOTREG)).","ExceptionType":"System.Runtime.InteropServices.COMException","StackTrace":" at
System.RuntimeTypeHandle.CreateInstance(RuntimeType type, Boolean publicOnly, Boolean noCheck, Boolean& canBeCached,
RuntimeMethodHandleInternal& ctor, Boolean& bNeedSecurityCheck)\r\n at System.RuntimeType.CreateInstanceSlow(Boolean
publicOnly, Boolean skipCheckThis, Boolean fillCache, StackCrawlMark& stackMark)\r\n at
System.Activator.CreateInstance(Type type, Boolean nonPublic)\r\n at System.Activator.CreateInstance(Type type)\r\n at
GSWebAPI.Controller.GSConfigController..ctor()\r\n at lambda_method(Closure )\r\n at
System.Web.Http.Dispatcher.DefaultHttpControllerActivator.Create(HttpRequestMessage request, HttpControllerDescriptor
controllerDescriptor, Type controllerType)"}}
At line:1 char:1
+ curl http://localhost:8080/api/Hello/HelloMethod
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : InvalidOperation: (System.Net.HttpWebRequest:HttpWebRequest) [Invoke-WebRequest], WebExc
   eption
    + FullyQualifiedErrorId : WebCmdletWebResponseException,Microsoft.PowerShell.Commands.InvokeWebRequestCommand

1 个答案:

答案 0 :(得分:0)

现在已解决,仅需要以下内容。谢谢大家的投入

右键单击“应用程序池”,然后选择“高级设置”-在“常规”部分中,将“启用32位应用程序”设置为True