0
votes

.net core 3.0 web app with razorpages and api

My api controllers are in a folder "/api" and one of them has a default Get method that I want to call with both a route parameter AND 2 other values

[HttpGet("{customer}")]
public IActionResult Get(string customer, string size, string color)

Trying to route using a parameter to a path like this
http get localhost://site-root/api/mycontroller/ABC?size=val1&color=val2
Where "ABC" would get mapped to a route parameter "customer" in my controller but this wont route.

... in configured routing in Startup

 app.UseEndpoints(endpoints =>
    {
        endpoints.MapControllers();
        endpoints.MapControllerRoute(name: "api", pattern: "api/[controller]/[action]");
        endpoints.MapRazorPages();
    });
1

1 Answers

0
votes

The problem seems to be in how you are registering the route. It should be:

 app.UseEndpoints(endpoints =>
    {
        endpoints.MapControllers();
        endpoints.MapControllerRoute(name: "api", pattern: "api/[controller]");
        endpoints.MapRazorPages();
    });

In your code, you are expecting an "[action]", therefore, the "ABC" text is being mapped as an action and not as an argument to the function.