如何使用Odata v4在C#中配置WEB API以接收多个POST em同一控制器

时间:2018-03-22 20:59:49

标签: c# rest api post odata

我在C#中有一个使用Odata V4的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()函数(&#34;学生&#34)。返回();

邮递员的错误:

error in postman

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

1 个答案:

答案 0 :(得分:0)

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

喜欢这个。

[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是非常好的,我认为:)