I have integrated a Next.js (v14) frontend with an ASP.NET Core 8 Web API backend. I integration seems working However, when I comment out when I run the server the get API on Swagger gives the Status code 404 response.
I tried everything but things aren’t working.
using Microsoft.AspNetCore.SpaServices.AngularCli;
using System.Diagnostics;
namespace Next_API
{
public class Program
{
public static void Main(string[] args)
{
var builder = WebApplication.CreateBuilder(args);
// Add services to the container.
builder.Services.AddControllers();
builder.Services.AddSpaStaticFiles(configuration =>
{
configuration.RootPath = "ClientApp/.next";
});
// Learn more about configuring Swagger/OpenAPI at https://aka.ms/aspnetcore/swashbuckle
builder.Services.AddEndpointsApiExplorer();
builder.Services.AddSwaggerGen();
var app = builder.Build();
if (app.Environment.IsDevelopment())
{
app.UseSwagger();
app.UseSwaggerUI();
app.UseDeveloperExceptionPage();
var processStartInfo = new ProcessStartInfo
{
FileName = "cmd.exe",
Arguments = "/c npm run dev",
WorkingDirectory = Path.Combine(Directory.GetCurrentDirectory(), "ClientApp"),
RedirectStandardOutput = true,
RedirectStandardError = true,
UseShellExecute = false,
CreateNoWindow = true
};
var process = Process.Start(processStartInfo);
process.OutputDataReceived += (sender, args) => Console.WriteLine(args.Data);
process.ErrorDataReceived += (sender, args) => Console.WriteLine(args.Data);
process.BeginOutputReadLine();
process.BeginErrorReadLine();
app.UseSpa(spa =>
{
spa.UseProxyToSpaDevelopmentServer("http://localhost:3000");
});
}
else
{
app.UseExceptionHandler("/Error");
app.UseHsts();
app.UseStaticFiles();
app.UseSpaStaticFiles();
app.UseSpa(spa =>
{
spa.Options.SourcePath = "ClientApp";
});
}
app.UseRouting();
app.MapControllers();
app.Run();
}
}
}
The API is not working status code 404 is the response everytime
New contributor
Code Red is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.