© 2026 Hedgehog Software, LLC

TwitterGitHubDiscord
More
CommunitiesDocsAboutTermsPrivacy
Search
Star
Setup for Free
C#C
C#•3y ago•
23 replies
kunio_kun

❔ Serving SPA on ASP.NET Core MVC Web API

Hi, I'm trying to serve SPA in a folder
client
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");
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");
    }
}
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'.
FileNotFoundException: Could not find file 'ProjectDirectory\bin\Debug\net7.0\client'.


How do I do this correctly? Thanks in advance.
C# banner
C#Join
We are a programming server aimed at coders discussing everything related to C# (CSharp) and .NET.
61,871Members
Resources

Similar Threads

Was this page helpful?
Recent Announcements

Similar Threads

ASP.NET CORE MVC VS ASP.NET CORE WEB API
C#CC# / help
17mo ago
✅ ASP.NET Core Web (MVC) Project
C#CC# / help
16mo ago
✅ ASP.Net Core Web API
C#CC# / help
6mo ago
Web Api request in asp.net core mvc project
C#CC# / help
3y ago