WebApi OData删除路由约定

时间:2015-02-11 09:15:34

标签: c# asp.net-web-api routing odata

我有一个MVVM项目。 对于模型,我首先使用EF6.0.0代码和WebApi。

总的来说,一切都运行良好,但有一点。

当我删除时,组成以下URL

http://localhost:50346/Recruiters/Addresses(guid'5d778c9d-56b2-449b-b655-22489e01636d')/ 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进行测试。

我错过了什么吗?

亲切的问候

的Jeroen

2 个答案:

答案 0 :(得分:0)

的Jeroen

我认为你所做的事情可以发挥作用。

但是,根据您的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 

为什么要写&#34; Recruiters&#34;

以下是使用所有示例代码的调试信息: enter image description here

此外,为什么要在演员Uri上发出**删除**请求?看来最后一个类型的案例段(即:CIP.Models.Domain.Addresses)是不是必要?

您可以参考OData Spec中的删除实体部分。如果您发现Web API OData存在任何问题,可以直接在WebApi OData On Github上提交问题。 感谢。

感谢。

答案 1 :(得分:0)

除了代码之外几乎没有小错误。

事实证明问题出在我的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>  

的Jeroen

相关问题