.net attribute routes not matched in production

Viewed 13

I have a asp.net v6 mvc project with an spa front end that works fine in development. (Following the new pattern where my spa dev server has a proxy configuration to forward api requests to the .net app).

When I deploy this to IIS, my api routes (all beginning "api/") are no longer recognised, and all api requests are redirected to the fallback spa home page. What's going on ?

This is my Configure method, in Startup.cs

public void Configure(IApplicationBuilder builder, IWebHostEnvironment env)
{
    ...
    builder.UseStaticFiles();
    builder.UseRouting();

    builder.UseEndpoints(endpoints =>
    {
        // I added this line to try and fix my problem. It's not necessary in dev, but still broken in prod.
        endpoints.MapControllers();
        // This was all I had in dev, but all my Controller actions have AspNetCore.Mvc Route attributes.
        endpoints.MapControllerRoute(
            name: "default",
            pattern: "api/{controller}/{action=Index}/{id?}");

        // I know this method is called and used, because if I change "index.html" everything breaks and I'm no longer redirected to my spa
        endpoints.MapFallbackToFile("index.html");
    });
}

Here is a controller...

using Microsoft.AspNetCore.Mvc;

[ApiController]
public class AllItemsController : ControllerBase
{
    [Route("api/all-items")]
    public ApiResponse<List<Item>, string> Get()
    {
0 Answers
Related