【问题标题】:Error occurred when trying to create a controller of type 'XYZController'. Make sure controller has a parameterless public constructor尝试创建类型为“XYZController”的控制器时发生错误。确保控制器有一个无参数的公共构造函数
【发布时间】:2019-01-08 11:29:55
【问题描述】:

以下代码在从 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

【问题讨论】:

  • 您确定该类中没有其他构造函数吗? GSConfig呢?
  • 提供控制器的实际表示。
  • 对不起,请忽略。已更正。
  • 如果没有minimal reproducible example 就无法提供太多帮助。
  • 我相信您的问题可能在于依赖解决方案...您使用的是哪种 IOC 容器?也许您忘记使用它注册内置解析器。 // 创建http配置时注册内置的web api分辨率 config.DependencyResolver = new WebApiDependencyResolver(Container);在这里阅读:docs.microsoft.com/en-us/previous-versions/aspnet/…

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


【解决方案1】:

现在已解决,只需要以下操作。感谢大家的投入

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

【讨论】:

    猜你喜欢
    • 2022-11-04
    • 2019-04-13
    • 2017-12-22
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-07-31
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多