c# - Azure 网站上的 Odata/Web Api 错误 "Make sure that the controller has a parameterless public constructor"

标签 c# azure odata

我在部署后访问 api 时从 azure 网站获取了此信息。它在本地运行得很好。

尝试创建“ItemController”类型的 Controller 时发生错误。确保 Controller 具有无参数公共(public)构造函数。

<Error><Message>An error has occurred.</Message><ExceptionMessage>An error occurred when trying to create a controller of type 'ItemController'. **Make sure that the controller has a parameterless public constructor.**</ExceptionMessage><ExceptionType>System.InvalidOperationException</ExceptionType><StackTrace>   at System.Web.Http.Dispatcher.DefaultHttpControllerActivator.Create(HttpRequestMessage request, HttpControllerDescriptor controllerDescriptor, Type controllerType)
   at System.Web.Http.Controllers.HttpControllerDescriptor.CreateController(HttpRequestMessage request)
   at System.Web.Http.Dispatcher.HttpControllerDispatcher.SendAsyncCore(HttpRequestMessage request, CancellationToken cancellationToken)
   at System.Web.Http.Dispatcher.HttpControllerDispatcher.&lt;SendAsync&gt;d__0.MoveNext()</StackTrace><InnerException><Message>An error has occurred.</Message><ExceptionMessage>**Could not load file or assembly 'EntityFramework, Version=6.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089' or one of its dependencies. The module was expected to contain an assembly manifest**.</ExceptionMessage><ExceptionType>System.BadImageFormatException</ExceptionType><StackTrace>   at RecycleMeOdataWebApi.Controllers.ItemController..ctor()
   at lambda_method(Closure )
   at System.Web.Http.Dispatcher.DefaultHttpControllerActivator.Create(HttpRequestMessage request, HttpControllerDescriptor controllerDescriptor, Type controllerType)</StackTrace></InnerException></Error>

  public partial class ItemController : ODataController
{
    public ItemController()
    {
    }
    private RecycleMeContext db = new RecycleMeContext();

    // GET odata/Item
    [Queryable]
    public IQueryable<Item> GetItem()
    {
        return db.Items;
    }

最佳答案

您的 Controller 只是缺少无参数公共(public)构造函数。

只需添加:

public ItemController() 
{
}

它应该可以工作。

关于c# - Azure 网站上的 Odata/Web Api 错误 "Make sure that the controller has a parameterless public constructor",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23866649/

相关文章:

c# - 避免多余的条件语句

javascript - 使用 REST API 在 SharePoint Online 中外部共享文件夹

c# - wcf Rest 中的身份验证

c# - 触发器中无法识别 Setter TargetName 'ContentPresenter'

c# - ASP.net session 登录

c# - 如何使用 SDK 2.4 扩展 Azure 云服务?

azure - Autorest生成需要beta包的Client Sdk

oauth - 使用 BreezeJS OData 数据服务传递授权持有者 token

c# - 如何在运行时动态地将值传递给 ObjectDataProvider.MethodParameters

powershell - 查询 Azure Active Directory 的 UPN 和主 SMTP 地址,然后导出到 CSV