C#C
C#3y ago
kunio_kun

❔ Serving SPA on ASP.NET Core MVC Web API

Hi, I'm trying to serve SPA in a folder client in BaseDirectory. I want it to be served in "/".

Here's what I tried

WebApplication app = builder.Build();
app.UseCors(options => {
    options.AllowAnyOrigin();
    options.AllowAnyHeader();
    options.AllowAnyMethod();
});
app.UseStaticFiles();

app.UseRouting();
app.UseAuthentication();
app.UseAuthorization();

// Configure the HTTP request pipeline.
app.UseSwagger();
app.UseSwaggerUI();

app.MapHub<DevicesHub>("api/hubs/devices");
app.MapControllers();
app.UseSpa(spa => {
    spa.Options.SourcePath = Path.Join(app.Environment.ContentRootPath, "client");
});
app.MapFallbackToController("Index", "Fallback");


And the fallback controller implementation is
public class Fallback: ControllerBase
{
    public IActionResult Index()
    {
        return File("~/client/index.html", "text/html");
    }
}


But when i navigate to /, I am getting
FileNotFoundException: Could not find file 'ProjectDirectory\bin\Debug\net7.0\client'.


How do I do this correctly? Thanks in advance.
Was this page helpful?