[英]WebApi OData only able to POST and GET entities
I have created a simple CRUD application using WebApi OData. 我已经使用WebApi OData创建了一个简单的CRUD应用程序。 I have been using the Composer tab of Fiddler 2 to test the service and no matter what I do I keep getting an HTTP 404 reponse from my local IIS server. 我一直在使用Fiddler 2的Composer选项卡来测试服务,无论我做什么,我始终从本地IIS服务器获得HTTP 404响应。 As stated a POST request works: 如前所述, POST请求有效:
http://mymachine/Service.Facade.Reports/odata/Reports
User-Agent: Fiddler 用户代理:提琴手
Content-Type: application/json 内容类型:application / json
Host: localhost 主机:localhost
Content-Length: 1373 内容长度:1373
{ "Name":"Report...","Group":"Hhowd444","ReportLayoutID":8270,"DatabaseInstanceID":1042 } {“名称”:“ Report ...”,“组”:“ Hhowd444”,“ ReportLayoutID”:8270,“ DatabaseInstanceID”:1042}
So I tried: 所以我尝试了:
However, changing the URL to: >
http://mymachine/Service.Facade.Reports/odata/Reports(8270)
and executing a PUT does not work 但是,将URL更改为:>http://mymachine/Service.Facade.Reports/odata/Reports(8270)
并执行PUT不起作用
The PUT leads to a 404 error. PUT导致404错误。 My attempts to debug and trace the service have led nowhere. 我调试和跟踪服务的尝试无济于事。
In the WebApiConfig class: 在WebApiConfig类中:
public static void Register(HttpConfiguration config)
{
ODataModelBuilder builder = new ODataConventionModelBuilder();
builder.EntitySet<ReportLayout>("Reports");
builder.EntitySet<ReportLayoutData>("ReportLayoutData");
builder.EntitySet<DatabaseInstance>("DataSources");
builder.EntitySet<DataSourceObject>("DataSourceObject");
config.Routes.MapODataRoute("odata", "odata", builder.GetEdmModel());
config.MapHttpAttributeRoutes();
config.Routes.MapHttpRoute(
name: "DefaultApi",
routeTemplate: "api/{controller}/{key}",
defaults: new { key = RouteParameter.Optional }
);
}
The Post and Put from the controller: 来自控制器的发布和放置:
public async Task<IHttpActionResult> Post(ReportLayout reportlayout)
{
if (!ModelState.IsValid)
{
return BadRequest(ModelState);
}
db.Reports.Add(reportlayout);
db.ReportsData.AddRange(reportlayout.LayoutData);
await db.SaveChangesAsync();
return Created(reportlayout);
}
public async Task<IHttpActionResult> Put([FromODataUri] int key, ReportLayout reportlayout)
{
if (!ModelState.IsValid)
{
return BadRequest(ModelState);
}
if (key != reportlayout.ReportLayoutID)
{
return BadRequest();
}
db.Entry(reportlayout).State = EntityState.Modified;
try
{
await db.SaveChangesAsync();
}
catch (DbUpdateConcurrencyException)
{
if (!ReportLayoutExists(key))
{
return NotFound();
}
else
{
throw;
}
}
return Updated(reportlayout);
}
In the end the code was not the issue but the configuration of my IIS 7.5 server. 最后,代码不是问题,而是IIS 7.5服务器的配置。
To resolve go to: 解决方法:
Everything works now after some pain and suffering! 经过一番痛苦和磨难,现在一切正常!
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.