2017-09-11 8 views
0

REST APIasp.net MVC 4.5に作成しています。私はMySQLデータベースを使用しています。私はasp.netとMySQL DBを実行するために必要な各ステップをたどってきました。 API私は次のエラーが表示されます。asp.netのREST API mvc 4.5

{"Message":"An error has occurred.","ExceptionMessage":"An error occurred when trying to create a controller of type 'ProductsController'. 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.Controllers.HttpControllerDescriptor.CreateController(HttpRequestMessage request)\r\n at System.Web.Http.Dispatcher.HttpControllerDispatcher.<SendAsync>d__1.MoveNext()","InnerException":{"Message":"An error has occurred.","ExceptionMessage":"Could not load file or assembly 'EntityFramework, Version=5.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089' or one of its dependencies. The located assembly's manifest definition does not match the assembly reference. (Exception from HRESULT: 0x80131040)","ExceptionType":"System.IO.FileLoadException","StackTrace":" at RestWithMySQL.Controllers.ProductsController..ctor()\r\n at lambda_method(Closure)\r\n at System.Web.Http.Dispatcher.DefaultHttpControllerActivator.Create(HttpRequestMessage request, HttpControllerDescriptor controllerDescriptor, Type controllerType)"}}

以下は私のweb.configファイル

<add name="ProductEntities" 
    connectionString= "metadata=res://*/ProductsModel.csdl|res://*/ProductsModel.ssdl|res://*/Products 
    Model.msl;provider=MySql.Data.MySqlClient;provider connection string=&quot;server=localhost;user id=root;database=accurate_dev;integrated security=True;MultipleActiveResultSets=True;App=EntityFramework&quot;" providerName="System.Data.EntityClient"/> 

以下は私のコントローラのコード

public class ProductsController : ApiController 
{ 
    public ProductEntities Entities = new ProductEntities(); 

    public HttpResponseMessage Get() 
    { 
     try 
     { 
      return Request.CreateResponse(HttpStatusCode.Found, Entities.products.ToList()); 
     } 
     catch (Exception) 
     { 
      return Request.CreateErrorResponse(HttpStatusCode.NotFound, "No Data found"); 
     } 
    } 

    public HttpResponseMessage Get(int id) 
    { 
     try 
     { 
      return Request.CreateResponse(HttpStatusCode.Found, Entities.products.SingleOrDefault(p => p.Id == id)); 
     } 
     catch (Exception) 
     { 
      return Request.CreateErrorResponse(HttpStatusCode.NotFound, "No Data found"); 
     } 
    } 

    public HttpResponseMessage Post([FromBody] product product) 
    { 
     try 
     { 
      Entities.products.Add(product); 
      Entities.SaveChanges(); 
      var response = Request.CreateResponse(HttpStatusCode.Created, product); 
      response.Headers.Location = Request.RequestUri; 

      return response; 
     } 
     catch (Exception) 
     { 
      return Request.CreateErrorResponse(HttpStatusCode.BadRequest, "Data not inserted"); 
     } 
    } 

    public HttpResponseMessage Delete(int id) 
    { 
     try 
     { 
      var product = Entities.products.SingleOrDefault(p => p.Id == id); 
      if (product == null) 
       return Request.CreateErrorResponse(HttpStatusCode.BadRequest, "Product not found to delete"); 
      Entities.products.Remove(product); 
      Entities.SaveChanges(); 

      return Request.CreateResponse(HttpStatusCode.OK, "Product Deleted Successfully"); 
     } 
     catch (Exception) 
     { 
      return Request.CreateErrorResponse(HttpStatusCode.BadRequest, "Product not deleted"); 
     } 
    } 

    public HttpResponseMessage Put(int id, [FromBody] product product) 
    { 
     try 
     { 
      var entity = Entities.products.SingleOrDefault(p => p.Id == id); 
      if (entity == null) 
       return Request.CreateErrorResponse(HttpStatusCode.BadRequest, "Product not found "); 

      entity.Category = product.Category; 
      entity.Name = product.Name; 
      entity.Price = product.Price; 
      Entities.SaveChanges(); 

      return Request.CreateResponse(HttpStatusCode.OK, "Product Updated Successfully"); 
     } 
     catch (Exception ex) 
     { 
      return Request.CreateErrorResponse(HttpStatusCode.BadRequest, ex); 
     } 
    } 
} 

私は私が間違っているのかわからないです。私はthisのサンプルコードを覚えています。また、dot netのパッケージをMySQLからインストールしました。

ご協力いただければ幸いです。

答えて

0

タイプ 'ProductsController'のコントローラを作成しようとしたときにエラーが発生しました。 ASP.NETは、お使いのコントローラのインスタンスを作成しようとしたときにエラーメッセージが示すように、コントローラは、パラメータなしのパブリックコンストラクタ

を持っていることを確認し、エラーが発生しました。だからあなたのProductsControllerファイルに空白のコンストラクタを追加してみてください:

public ProductsController() { } 

さて、この誤差を考えると、私はあなたの親クラス(ApiController)を想定も(そうでない場合はASP.NETの代わりにすることをを使用していたパラメータなしのコンストラクタを持っていませんこのエラーをスローする)。したがって、基本コンストラクタにいくつかのデフォルトパラメータを渡す必要があるでしょう:

public ProductsController() : base("Some", "Parameters") { } 

幸運を祈る!

編集:あなたはリンク先のガイドを読んだだけです。それがすべてだった場合は、おそらくApiControllerはまったく必要ありません(少なくとも私はそのガイドにそれについて言及していません)。したがって、上記の提案がうまくいかない場合は、クラス宣言から: ApiController部分を削除してみてください。

+0

':ApiController'は既に' MSSQL DB'で試してみたので削除できません。私がそれを削除すると、私は 'request'エラーを受け取ります –

関連する問題