为什么我的 OData POST body 模型不起作用?

Why is my OData POST body to model not working?

我在 ASP.NET 应用程序中使用 OData v4。每次我使用 Postman post 到我的端点时,我都会收到以下错误。问题在于这一行 public IHttpActionResult Post([FromBody] HolidayPost data),因为当我删除 [FromBody] HolidayPost data 时该函数将 运行。我似乎无法让我的 POST body 与 HolidayPost 模型保持一致。我不确定我做错了什么。

留言

"No MediaTypeFormatter is available to read an object of type 'HolidayPost' from content with media type 'application/json'.",

类型

System.Net.Http.UnsupportedMediaTypeException

堆栈跟踪

" at System.Net.Http.HttpContentExtensions.ReadAsAsync[T](HttpContent content, Type type, IEnumerable1 formatters, IFormatterLogger formatterLogger, CancellationToken cancellationToken)\r\n at System.Web.Http.ModelBinding.FormatterParameterBinding.ReadContentAsync(HttpRequestMessage request, Type type, IEnumerable1 formatters, IFormatterLogger formatterLogger, CancellationToken cancellationToken)"

我的邮递员POST请求

{
    "Dates": "[\"2022-01-20T00:00:00.000-06:00\",\"2022-02-20T00:00:00.000-06:00\",\"2022-03-23T00:00:00.000-05:00\"]",
    "apikey": "XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX"
}

WebApiConfig.cs

namespace PortalogicOData
{
    public static class WebApiConfig
    {
        public static void Register(HttpConfiguration config)
        {
            config.EnableCors();

            // Create OData entities
            ODataModelBuilder builder = new ODataConventionModelBuilder();
            config.Count().Filter().OrderBy().Expand().Select().MaxTop(null); // enable OData Model Bound Attributes
            config.Formatters.JsonFormatter.SupportedMediaTypes.Add(new MediaTypeHeaderValue("application/json"));

            builder.EntitySet<Holiday>("Holidays");

            // map entities
            config.MapODataServiceRoute("ODataRoute", null, builder.GetEdmModel());
        }
    }
}

HolidaysController.cs

namespace PortalogicOData.Controllers
{
    [EnableCors("*", "*", "*")]
    public class HolidaysController : ODataController
    {
        private readonly HolidayService m_service = new HolidayService();

        [EnableQuery]
        public IList<Holiday> Get([FromUri] string apikey)
        {
            if (GenericHelper.ValidateApi(apikey))
            {
                return m_service.Getdata();  // this works fine
            }
            else
            {
                return null;
            }
        }

        [HttpPost]
        public IHttpActionResult Post([FromBody] HolidayPost data)
        {
            // this function fails to execute everytime
        }
    }
}

HolidaysService.cs

namespace PortalogicOData.Business
{
    public class HolidayService
    {

        public List<Holiday> Holidays => Getdata();

        public List<Holiday> Getdata()
        {
            // this works fine
        }

        public Holiday Update(HolidayPost data)
        {
            // my update code
        }

    }
}

HolidayPost.cs

namespace PortalogicOData.Models
{
    public class HolidayPost
    {
        public string apikey { get; set; }
        public string Dates { get; set; }

    }
}

Holiday.cs

namespace PortalogicOData.Models
{
    public class Holiday
    {
        public int HolidayID { get; set; }
        public string FunctionResult { get; set; }
        public int ReturnCode { get; set; }
        public DateTime Date { get; set; }
    }
}

显然,OData ASP.NET 强制您只允许正文采用路由类型的格式(例如 Holiday 而不是 HolidayPost)。我不得不创建一个用于帖子的通用 class 路径。这比弄清楚如何使用 OData 操作和函数要容易得多。