我有一个MVVM项目。对于模型,我首先使用EF6.0.0代码和WebApi。
一般来说,除了一件事外,一切都很好。
当我执行删除操作时,将生成以下URL
http://localhost:50346/Recruiters/Addresses(guid/CIP.Models.Domain.Addresses
这会导致404错误。
因此,我创建了这样的路由约定:
using Microsoft.Data.Edm;
using System;
using System.Collections.Generic;
using System.Diagnostics.Contracts;
using System.Linq;
using System.Net.Http;
using System.Web;
using System.Web.Http.Controllers;
using System.Web.Http.OData.Routing;
using System.Web.Http.OData.Routing.Conventions;
namespace CIP
{
public class AddressesRoutingConvention : EntitySetRoutingConvention
{
public override string SelectAction(ODataPath odataPath, HttpControllerContext controllerContext, ILookup<string, HttpActionDescriptor> actionMap)
{
if (odataPath.PathTemplate == "~/entityset/key/cast")
{
HttpMethod httpMethod = controllerContext.Request.Method;
string httpMethodName;
switch (httpMethod.ToString().ToUpperInvariant())
{
case "DELETE":
httpMethodName = "Delete";
break;
default:
return null;
}
Contract.Assert(httpMethodName != null);
IEdmEntityType entityType = odataPath.EdmType as IEdmEntityType;
string actionName = httpMethodName + entityType.Name;
if (actionName != null)
{
KeyValuePathSegment keyValueSegment = odataPath.Segments[1] as KeyValuePathSegment;
controllerContext.RouteData.Values[ODataRouteConstants.Key] = keyValueSegment.Value;
return actionName;
}
}
// Not a match
return null;
}
}
}并加入了这条路线
var conventions = ODataRoutingConventions.CreateDefault();
conventions.Insert(0, new AddressesRoutingConvention());
config.Routes.MapODataRoute("Addresses", "Addresses", addressesBuilder.GetEdmModel(), new DefaultODataPathHandler(), conventions);`在控制器里
public async Task<IHttpActionResult> DeleteAddresses([FromODataUri] Guid key)
{
Addresses addresses = await db.Addresses.FindAsync(key);
if (addresses == null)
{
return NotFound();
}
db.Addresses.Remove(addresses);
await db.SaveChangesAsync();
return StatusCode(HttpStatusCode.NoContent);
}但我仍然得到404错误。
我试着用同样的结果从SOAPUI中测试它。
我是不是遗漏了什么?
亲切的问候
耶伦
发布于 2015-02-17 08:07:11
除了代码之外,很少有小错误是可以的。
原来问题出在我的web.config上。在将下面的配置添加到它之后,所有东西都可以工作,甚至断点:
<system.webServer>
<handlers>
<clear/>
<add name="ExtensionlessUrlHandler-Integrated-4.0" path="/*"
verb="*" type="System.Web.Handlers.TransferRequestHandler"
preCondition="integratedMode,runtimeVersionv4.0" />
</handlers>
</system.webServer> 耶伦
发布于 2015-02-15 01:53:53
耶伦
我认为你所做的一切都能奏效。
但是,根据OData路由设置,下面的请求Uri是不正确的:
http://localhost:50346/Recruiters/Addresses(guid'5d778c9d-56b2-449b-b655-22489e01636d')/CIP.Models.Domain.Addresses 因为您的OData路由设置是:
config.Routes.MapODataRoute("Addresses", "Addresses", addressesBuilder.GetEdmModel(), ...因此,您的请求Uri应该是:
http://localhost:50346/Addresses/Addresses(guid'5d778c9d-56b2-449b-b655-22489e01636d')/CIP.Models.Domain.Addresses 你为什么写“招聘人员”?
下面是使用所有示例代码的调试信息:

此外,为什么在强制转换Uri上发出*Delete**请求?似乎没有必要使用最后一个类型的case段(即: CIP.Models.Domain.Addresses)?
您可以从OData规范中删除一个实体部分。如果您发现Web的任何问题,可以直接在WebApi OData On Github上提交一个问题。谢谢。
谢谢。
https://stackoverflow.com/questions/28450469
复制相似问题