Search code examples
angularasp.net-core-webapiangular17asp.net-core-8

Unable to get non default method from the controller in ASP.NET Core 8 with Angular frontend


I am using ASP.NET Core 8.0 with Angular v17+ frontend. I can only get my default get method from the controller but not my other one.

My controller looks like this:

[Route("api/values")]
[ApiController]
public class ValuesController : ControllerBase
{
    // GET: api/<ValuesController>
    [HttpGet]
    public ActionResult<IEnumerable<string>> Get()
    {
        return Ok(new string[] { "ActionResult<IEnumerable<string>> Get()" });
    }

    [HttpGet]
    [Route("getme")]
    public ActionResult<IEnumerable<string>> GetMe()
    {
        return Ok(new string[] { Guid.NewGuid().ToString() + " | " + Guid.NewGuid().ToString() });
    }
}

I can get the first method, but the second GET method I get a

Http failure during parsing for http://localhost:4200/api/values/getme

Not sure what I am doing wrong. I did test it using the full path and that works for some reason.

This is my proxy.config.json:

{
  "/api/*": {
    "target": "https://localhost:44351",
    "secure": false,
    "logLevel": "debug"
  }
}

Solution

  • After hours of trying to solve this. This is what I did to fix it

    Original proxy.config.json

    {
      "/api/*": {
        "target": "https://localhost:44351",
        "secure": false,
        "verbose": true
      }
    }
    

    Updated proxy.config.json

    {
      "/api/**": {
        "target": "https://localhost:44351",
        "secure": false,
        "verbose": true
      }
    }
    

    Difference is that extra *

    • "/api/*" matches any request directly under /api/. For example, it matches /api/values but not /api/values/getme.
    • "/api/"** matches any request under /api/ at any level of depth. For example, it matches both /api/values and /api/values/getme.

    Your welcome to ever else finds this useful.