REST API
をasp.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="server=localhost;user id=root;database=accurate_dev;integrated security=True;MultipleActiveResultSets=True;App=EntityFramework"" 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
からインストールしました。
ご協力いただければ幸いです。
':ApiController'は既に' MSSQL DB'で試してみたので削除できません。私がそれを削除すると、私は 'request'エラーを受け取ります –