使用GUID主键的OData V4 REST

Pet*_*ete 2 rest guid odata

所有!我正在使用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
Run Code Online (Sandbox Code Playgroud)

这是我的控制器的代码......

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);
    }
}
}
Run Code Online (Sandbox Code Playgroud)

小智 6

试试这个: http:// localhost/ershubrest/AppVersions( guid'00000000-e90f-4938-b8f6-000000000000')

这应该工作!!


Yi *_*SFT 5

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

http://localhost/ershubrest/AppVersions(00000000-e90f-4938-b8f6-000000000000)
Run Code Online (Sandbox Code Playgroud)

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

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