Search code examples
asp.net-coreblazorasp.net-web-api-routingasp-net-core-spa-services

Why can I only call one action on a controller Blazor / aspnet core 3?


So I have two routes in a controller with 2 gets. When trying to call it from Razor only the first one is called. I'm not sure if I need to add something to the startup or an attribute.

    [ApiController]
    [Route("[controller]")]
    public class TestController : BaseController
    {
             [HttpGet]        
            public async Task<IActionResult> Index()
            {
                     return Content("1");
            }        

            [Route("/callback")]
            [HttpGet("[controller]/[action]")]
            public async Task<IActionResult> Callback(string state, string code)
            {
                         return Content("2");
            }
     }

In my startup I have the following endpoints configured

app.UseEndpoints(endpoints =>
            {
                endpoints.MapRazorPages();
                endpoints.MapControllers();             
                endpoints.MapFallbackToFile("index.html");
            });

I then use some script in my razor file code section to call the controller

 var httpClient = new HttpClient();
 httpClient.BaseAddress = new Uri(Navigation.BaseUri);
 var response  = await  httpClient.GetAsync($"Test/callback?code={Code}&state={State}");

However only the Index route can be called


Solution

  • [Route("/callback")]
    

    should be

    [Route("/Test/callback")]