Userssp
Userssp

Reputation: 43

How to configure WEB API in C# with Odata v4 to receive more than one POST em same Controller

I have a WEB API project in C# that uses Odata V4, and I need a controller with more than one POST. How to configure? When i call my rest in postman, i have an error: "No non-OData HTTP route registered".

Here's my Controller code:

[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);
    }
}

I have a file that set routes with this line: builder.EntityType().Function("student").Returns();

My error in postman:

error in postman

I use this link to help me https://blogs.msdn.microsoft.com/odatateam/2014/12/08/tutorial-sample-functions-actions-in-web-api-v2-2-for-odata-v4-0-type-scenario/

Upvotes: 1

Views: 398

Answers (1)

Pavel B.
Pavel B.

Reputation: 885

you can try to use overflow method. just give to the method diferent input prams.

Like this.

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

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

It may works. And one typ try Swagger UI to test api is works very well i think :)

Upvotes: 0

Related Questions