asp.netasp.net-coreasp.net-core-3.0

ASP.NET Core 3.0 Web API - controller method not hit


I am new to ASP.NET Core Web API - this is my controller method:

[HttpPost]
[Route("Createnewlead")]
public IActionResult LeadCreate([FromBody]CRM_Lead Lead)
{
    // do stuff
}

This is my json:

{
   "RegionID": "1",
   "RunningNo": "1633",
   "CardName": "Google Pte Limited",
   "Telephone": "65748394",
   "Mobile": "89349859",
   "Fax": "47850555",
   "Email": "sre@hotmail.com",
   "ROC": "28IO45h44",
   "OwnerEmail": "huisan@syspex.com"
}

enter image description here

Please advise me!


Solution

  • Update your route to

    [Route("api/[Controller]/Createnewlead")]
    

    Your route is currently set to [Route("Createnewlead")] which will translate to route https://localhost:5001/createnewlead but you are calling https://localhost:5001/api/sap/createnewlead

    or alternatively, post to

     localhost:5001/Createnewlead
    

    In your example, you are setting a route per action, instead you can also set a route per Controller, for example

    [Route("api/[controller]/[action]")]
    public class MyController: Controller 
    {
    }
    

    or

    [Route("[controller]/[action]")]
    public class MyController: Controller 
    {
    }
    

    or

    [Route("[controller]")]
    public class MyController: Controller 
    {
    }
    

    It all depends on what you want your routing to look like