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?
– novic
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?
– Isa
And how would this process "with another route"?
– Isa