ODataController PUT无法正常工作

时间:2017-11-29 03:48:08

标签: c# odata

我已经实现了一个oDataController,除PUT动词外,一切都有效。 当我打电话给它时,无论请求中的内容是什么,我都会收到200响应。 当我在第一行放置一个断点时,它永远不会被击中。 似乎没有设置一个动词的路由。 即使我使Authorization标头无效,我仍然得到200 OK响应。 我该怎么做才能诊断出这个问题? 我正在使用Postman测试这个(我试过小提琴手重新发布PATCH作为PUT,仍然可以获得200 OK)

这是我的WebApiConfig

public static class WebApiConfig
{
    public static void Register(HttpConfiguration config)
    {
        config.MapODataServiceRoute(
            "oData routes for ...",
            "oData",
            GetEdmModel());
        config.EnsureInitialized();
    }

    private static Microsoft.OData.Edm.IEdmModel GetEdmModel()
    {
        var builder = new ODataConventionModelBuilder;
        var rpEntitySet = builder.EntitySet<RuralProfessional>("RuralProfessionals");
        var ceEntitySet = builder.EntitySet<CalendarEvent>("CalendarEvents");
        ceEntitySet.EntityType.HasKey(x => x.Key);
        var edmModel = builder.GetEdmModel();
        return edmModel;
    }
}

这是使用PUT方法的控制器

using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Threading.Tasks;
using System.Web.Http;
using System.Web.OData;
using Terabyte.UmbracoWebsite.Infrastructure.CalendarEvents;
using Terabyte.UmbracoWebsite.ViewModels.ImportEvents;

namespace Terabyte.UmbracoWebsite.Controllers.oData
{
    /// <summary>
    /// To test this you will need to build auth header:
    /// https://blogs.msdn.microsoft.com/odatateam/2010/07/21/odata-and-authentication-part-6-custom-basic-authentication/
    /// </summary>
    [Authorize]
    [EnableQuery]
    public class CalendarEventsController : ODataController
    {
        readonly CalendarEventsRepository _repository = new CalendarEventsRepository();

        [AllowAnonymous]
        public IQueryable<CalendarEvent> Get()
        {
            return _repository.GetAll();
        }

        /// <summary>
        /// example url: http://localhost:18903/odata/CalendarEvents(afbc13b6-84a5-4a3a-889c-1cf61f26c980)
        /// </summary>
        public SingleResult<CalendarEvent> Get([FromODataUri] Guid key)
        {
            var item = _repository.Find(key);
            return SingleResult.Create(new List<CalendarEvent>(){item}.AsQueryable());
        }

        public async Task<IHttpActionResult> Post(CalendarEvent item)
        {
            if (!ModelState.IsValid)
                return BadRequest(ModelState);
            var existing = _repository.Find(item.Key);
            if (existing == null)
            {
                _repository.Create(item);
                return Created(item);
            }
            else
            {
                _repository.Save(item);
                return Updated(item);
            }
        }

        public async Task<IHttpActionResult> Patch([FromODataUri] Guid key, Delta<CalendarEvent> delta)
        {
            if (!ModelState.IsValid)
                return BadRequest(ModelState);

            var entity = _repository.Find(key);
            if (entity == null)
                return NotFound();

            delta.Patch(entity);
            _repository.Save(entity);
            return Updated(entity);
        }

        /// <summary>
        /// !!!!!!!!!!!!!!!!!!!!!!!!!!!!! BROKEN ... this doesn't get triggered.
        /// </summary>
        [System.Web.Http.HttpPut]
        public async Task<IHttpActionResult> Put([FromODataUri] Guid key, CalendarEvent item)
        {
            if (!ModelState.IsValid || key != item.Key)
                return BadRequest();
            _repository.Save(item);
            return Updated(item);
        }

        public async Task<IHttpActionResult> Delete([FromODataUri] Guid key)
        {
            _repository.Delete(key);
            return StatusCode(HttpStatusCode.NoContent);
        }
    }
}

网址为:http://localhost:18903/odata/CalendarEvents(3da8432c-4c9f-40cc-9413-c82025720de8) 这适用于PATCH动词,但不适用于PIUT

0 个答案:

没有答案