Is it possible to have more than one "post" route in a controller. NET?

Asked

Viewed 61 times

1

I’m starting a C#. NET Core API project. When I create a controller like "API Controller with read/write actions" it creates the available actions.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Mvc;
namespace Service.Login.Controllers {
[Route("api/[controller]")]
[ApiController]
public class TestesController : ControllerBase
{
    // GET: api/<TestesController>
    [HttpGet]
    public IEnumerable<string> Get()
    {
        return new string[] { "value1", "value2" };
    }

    // GET api/<TestesController>/5
    [HttpGet("{id}")]
    public string Get(int id)
    {
        return "value";
    }

    // POST api/<TestesController>
    [HttpPost]
    public void Post([FromBody] string value)
    {
    }

    // PUT api/<TestesController>/5
    [HttpPut("{id}")]
    public void Put(int id, [FromBody] string value)
    {
    }

    // DELETE api/<TestesController>/5
    [HttpDelete("{id}")]
    public void Delete(int id)
    {
    }
}

}

How do I get 2 POST actions? For example:

// POST api/<TestesController>
[HttpPost]
public string PostA([FromBody] string value)
{
  return "A";
}

// POST api/<TestesController>
[HttpPost]
public int PostB([FromBody] string value)
{
  return 0;
}

I’ve already set the course in Startup.Cs...

 public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
    {
        if (env.IsDevelopment()) {app.UseDeveloperExceptionPage();}
        app.UseHttpsRedirection();
        app.UseResponseCompression();
        app.UseRouting();

        app.UseAuthorization();

        app.UseEndpoints(endpoints =>
        {
            endpoints.MapControllers();
            endpoints.MapControllerRoute(
                name: "default",
                pattern: "{controller}/{action}/{id?}"
            );
        });
    }
  • What’s the reason you wanted to do this, you can, with another route, but, what’s the main goal?

  • 1

    I’m testing what C# gives me of resources, is a test project so I don’t have exactly a reason for it. This is not good practice @Virgilionovic?

  • 1

    And how would this process "with another route"?

1 answer

0


When the template the default methods are created get, put, post and delete with their delegates specific programming. To add a new method post it is necessary to create a method that differs from others and that a different route is specified, example:

// método padrão
[HttpPost]
public IActionResult Post([FromBody] Source value)
{
    return Ok(new { @now = DateTime.Now, value });
}

// método que é diferente do padrão e que tem parâmetros diferentes
[HttpPost(), Route("service")]
public IActionResult Post([FromBody] Source value, string name = null)
{
    return Ok(new { Now = DateTime.Now, value,Time = DateTime.Now.TimeOfDay });
}

or rename the method with the route configuration.

[HttpPost(), Route("service")]
public IActionResult Post1([FromBody] Source value)
{
    return Ok(new { Now = DateTime.Now, value,Time = DateTime.Now.TimeOfDay });
}

to reach these two routes follows respectively the paths:

  • api/source
  • api/source/service

but, this is not very usual

Browser other questions tagged

You are not signed in. Login or sign up in order to post.