使用GUID主键的OData V4 REST

时间:2014-10-16 16:23:31

标签: rest odata guid

一切!我正在使用OData v4构建REST服务。我的表有一个GUID主键。

我的GET和POST请求工作正常。但PUT,PATCH和DELETE请求因404而失败。

我不确定网址应该是什么样子。我在Fiddler尝试了这些,都得到了404.我用Google搜索了这一点,没有运气。

http://localhost/ershubrest/AppVersions/guid'00000000-e90f-4938-b8f6-000000000000'

http://localhost/ershubrest/AppVersions/'00000000-e90f-4938-b8f6-000000000000'

http://localhost/ershubrest/AppVersions/00000000-e90f-4938-b8f6-000000000000

以下是我的控制器的代码......

using ERSHubRest.Models;
using System.Data.Entity;
using System.Data.Entity.Infrastructure;
using System.Linq;
using System.Net;
using System.Threading.Tasks;
using System.Web.Http;
using System.Web.OData;
using System.Web.OData.Query; 
using System.Web.OData.Routing;

namespace ERSHubRest.controllers
{
[ODataRoutePrefix("AppVersions")]
public class AppVersionsController : ODataController
{
    HubModel db = new HubModel();

    private bool AppVersionsExists(System.Guid key)
    {
        return db.AppVersions.Any(p => p.AppVersionId == key);
    }

    // http GET for select queries

    [ODataRoute]
    [EnableQuery]
    public IQueryable<AppVersions> Get()
    {
        return db.AppVersions;
    }

    [ODataRoute("({key})")]
    [EnableQuery]
    public IHttpActionResult Get([FromODataUri] System.Guid key)
    {
        IQueryable<AppVersions> result = db.AppVersions.Where(p => p.BusinessId == key);

        if (result == null)
        {
            return NotFound();
        }

        return Ok(result);
    }

    // http POST for insert

    [ODataRoute()]
    [HttpPost]
    [EnableQuery(AllowedQueryOptions = AllowedQueryOptions.All)]
    public async Task<IHttpActionResult> Post(AppVersions appVersions)
    {
        if (!ModelState.IsValid)
        {
            return BadRequest(ModelState);
        }
        db.AppVersions.Add(appVersions);
        await db.SaveChangesAsync();
        return Created(appVersions);
    }

    // http PUT and PATCH for updates

    [ODataRoute()]
    [HttpPatch]
    [EnableQuery(AllowedQueryOptions = AllowedQueryOptions.All)]
    public async Task<IHttpActionResult> Patch([FromODataUri] System.Guid key, Delta<AppVersions> appVersions)
    {
        if (!ModelState.IsValid)
        {
            return BadRequest(ModelState);
        }
        var entity = await db.AppVersions.FindAsync(key);
        if (entity == null)
        {
            return NotFound();
        }
        appVersions.Patch(entity);
        try
        {
            await db.SaveChangesAsync();
        }
        catch (DbUpdateConcurrencyException)
        {
            if (!AppVersionsExists(key) )
            {
                return NotFound();
            }
            else
            {
                throw;
            }
        }
        return Updated(entity);
    }

    [ODataRoute()]
    [HttpPut]
    [EnableQuery(AllowedQueryOptions = AllowedQueryOptions.All)]
    public async Task<IHttpActionResult> Put([FromODataUri] System.Guid key, AppVersions update)
    {
        if (!ModelState.IsValid)
        {
            return BadRequest(ModelState);
        }
        if ( ! key.Equals( update.BusinessId ))
        {
            return BadRequest();
        }
        if (!AppVersionsExists(key))
        {
            return BadRequest();
        }
        db.Entry(update).State = EntityState.Modified;
        try
        {
            await db.SaveChangesAsync();
        }
        catch (DbUpdateConcurrencyException)
        {
            if ( ! AppVersionsExists(key))
            {
                return NotFound();
            }
            else
            {
                throw;
            }
        }
        return Updated(update);
    }

    // last is Delete

    [ODataRoute()]
    [HttpDelete]
    [EnableQuery(AllowedQueryOptions = AllowedQueryOptions.All)]
    public async Task<IHttpActionResult> Delete([FromODataUri] System.Guid key)
    {
        var appVersions = await db.AppVersions.FindAsync(key);
        if (appVersions == null)
        {
            return NotFound();
        }
        db.AppVersions.Remove(appVersions);
        await db.SaveChangesAsync();
        return StatusCode(HttpStatusCode.NoContent);
    }

    // clean up 

    protected override void Dispose(bool disposing)
    {
        db.Dispose();
        base.Dispose(disposing);
    }
}
}

3 个答案:

答案 0 :(得分:5)

PATCH,PUT和DELETE的请求URL应为:

http://localhost/ershubrest/AppVersions(00000000-e90f-4938-b8f6-000000000000)

OData使用括号来使用键来寻址单个实体。

有关更多URL约定,可以参考OData V4 URL约定规范:http://docs.oasis-open.org/odata/odata/v4.0/os/part2-url-conventions/odata-v4.0-os-part2-url-conventions.html

答案 1 :(得分:5)

试试这个: http://localhost/ershubrest/AppVersions(guid&#39; 00000000-e90f-4938-b8f6-000000000000&#39)

那应该有用!!

答案 2 :(得分:2)

Odata V1-3:http://localhost/ershubrest/AppVersions(guid'00000000-e90f-4938-b8f6-000000000000')

Odata V4:http://localhost/ershubrest/AppVersions(00000000-e90f-4938-b8f6-000000000000)

我已经对Odata进行了为期2天的测试

我保证!