How to add separate default routes for GET and POST requests in ASP.NET Core 2.0 MVC?

My Startup.cs class has the following:

app.UseMvc(routes =>
    {
        routes.MapRoute("default", "api/{controller}/{action=Get}");
    });

All of my MVC controllers have a Get on the method, and some of them have a Post. Something like that:

public class ExampleController : Controller
{
    [HttpGet]
    public MyType Get()
    {
        return GetMyTypeFromSomewhere();
    }

    [HttpPost("api/[controller]")]
    public void Post([FromBody]MyType updated)
    {
        //do some stuff with the new instance
    }
}

Currently, I need to have the ("api/[controller]")Mail method so that messages can reach it.

I want to be able to remove this and redirect POST requests to the default Post server methods. The problem is that if I do this in the current state, the POST HTTP requests end up sending to /api/Example/Get.

MapGet MapPost, , RequestDelegate. .

?

, , GET POST , api/controller/action, {action}, -. , Get Post , =Get MapRoute

+4
1

, Get, :

routes.MapRoute("default", "api/{controller}/{action=Get}");

( ), .. /api/example/get, MVC . Get , URL-.

web api!

web api HttpVerb . HttpVerb URL. HttpVerb .

api /. , MVC ( RPC).

:

Startup.cs

public void ConfigureServices(IServiceCollection services)
{
    services.AddMvc();
}

public void Configure(IApplicationBuilder app, IHostingEnvironment env)
{
    app.UseMvcWithDefaultRoute();
}

[Route("api/[controller]")]
public class CustomersController : Controller 
{
    [HttpGet]
    public IActionResult Get()
    {   ...   }

    [HttpPost]
    public IActionResult Post()
    {   ...   }
}
+3

Source: https://habr.com/ru/post/1687325/


All Articles