Search code examples
asp.net-mvcpostasp.net-coreasp.net-web-apiget

Convert request from GET to POST in ASP.NET CORE APPLICATION


I need to convert the request from get to post in two contexts: - in the case of the standard controller - in the case of web api in an asp.net core c # project.

Here are the method declarations. Standard controller:

public async Task<IActionResult> Details(string id)

Web Api Controller:

[HttpGet("{deviceIdorId}/{action2}")] 
public async Task<IEnumerable<CosmosDBTelemetry>> GetAsync(string deviceIdorId,string action2)   

What should I do?

Thanks,

Simone


Solution

  • for convert HttpGet method to post you can create a view model to pass data. for example:

    Web Api

    public class ApiDto
    {
          public string deviceIdorId { get; set;}
          public string action2{ get; set;}
    }
    
    
    [HttpPost("ActionName")] 
    public async Task<IEnumerable<CosmosDBTelemetry>> ActionName(ApiDto dto)  
    

    Controller:

    public class IdDto
    {
          public string Id{ get; set;}
    }
    
    [HttpPost()]
    public async Task<IActionResult> Details(IdDto id)