【问题标题】:How to configure WEB API in C# with Odata v4 to receive more than one POST em same Controller如何在 C# 中使用 Odata v4 配置 WEB API 以接收多个 POST em 同一个控制器
【发布时间】:2018-03-22 20:59:49
【问题描述】:

我有一个使用 Odata V4 的 C# WEB API 项目,我需要一个具有多个 POST 的控制器。如何配置? 当我在邮递员中调用我的休息时,我有一个错误:“没有注册非 OData HTTP 路由”。

这是我的控制器代码:

[Authorize]
[ODataRoutePrefix("student")]
public class StudentController : ApiODataControllerBase<StudentViewModel, Student>
{
    private readonly IStudentAppService _studentService;
    private readonly ISchoolAppService _schoolAppService;

    public StudentController(IStudentAppService studentAppServiceBase, IStudentAppService studentService, ISchoolAppService schoolAppService)
        : base(studentAppServiceBase, studentService)
    {
        _studentService = studentService;
        _schoolAppService = schoolAppService;
    }

    [HttpPost]
    [ODataRoute]
    public async Task<IHttpActionResult> PostStudent([FromBody] StudentViewModel viewModel)
    {
        try
        {
            var student = await _studentService.Register(viewModel);
            return Response(student);
        }
        catch (Exception ex)
        {

            NotifyExceptionErrors(ex);
            return Response();
        }
    }

    [HttpPost]
    [ODataRoute]
    public async Task<IHttpActionResult> PostOtherSample([FromBody] StudentViewModel viewModel)
    {
        try
        {
            // sample: other register
            var student = await _studentService.Register(viewModel);
            return Response(student);
        }
        catch (Exception ex)
        {

            NotifyExceptionErrors(ex);
            return Response();
        }
    }

    public override async Task<IHttpActionResult> Post([FromBody] StudentViewModel viewModel)
    {
        return await Task.FromResult(Task<IHttpActionResult>.Factory.StartNew(() =>
        {
            NotifyError("BadRequest", "No access.");
            return Response();
        }).Result);
    }
}

我有一个用这一行设置路由的文件: builder.EntityType().Function("学生").Returns();

我在邮递员中的错误:

我使用此链接来帮助我 https://blogs.msdn.microsoft.com/odatateam/2014/12/08/tutorial-sample-functions-actions-in-web-api-v2-2-for-odata-v4-0-type-scenario/

【问题讨论】:

    标签: c# rest api post odata


    【解决方案1】:

    你可以尝试使用溢出方法。只需给方法不同的输入婴儿车。

    像这样。

    [HttpPost]
    [ODataRoute]
    public async Task<IHttpActionResult> PostOtherSample([FromBody] StudentViewModel viewModel){//your code}
    
    [HttpPost]
    [ODataRoute]
    public async Task<IHttpActionResult> PostOtherSample([FromBody] OtherViewModel viewModel){//your code}
    

    它可能有效。我认为一个典型的尝试 Swagger UI 来测试 api 效果很好:)

    【讨论】:

      猜你喜欢
      • 2017-12-04
      • 2015-12-15
      • 2014-09-02
      • 1970-01-01
      • 2014-10-01
      • 2014-07-18
      • 1970-01-01
      • 2016-05-11
      • 2016-02-28
      相关资源
      最近更新 更多