I am developing three web applications that are part of a larger system. These are single page applications with an API, and as such I have wildcard route for non API actions that points to my main controller.
For two of these applications this works fine but for the third the redirect doesn't work and I just get a 404 returned when trying to access a front end route. The setup as far as I can tell is identical and I am completely stumped as to why it is not working.
I am using screenshots rather than code snippets so I can show the setup side by side (working on the left, not working on the right).
As you can see the key sections of code are identical in both applications and yet one works and one doesn't.
What have I missed? Where else should I look? And any guidance on how I can debug this would be greatly appreciated.
.csproj
Startup.cs
This is the key bit of code which as you can see is identical in both places:
app.UseMvc(routes =>
{
routes.EnableDependencyInjection();
routes.MapRoute("default", "{controller=Home}/{action=Index}/{id?}");
});
app.MapWhen(x => !x.Request.Path.Value.StartsWith("/api", StringComparison.OrdinalIgnoreCase), builder =>
{
builder.UseMvc(routes =>
{
routes.MapRoute("spa-fallback", "{*url}", new { controller = "Home", action = "Index" });
});
});
Program.cs
Web.config
app.UseMvc(routes =>
{
routes.EnableDependencyInjection();
routes.MapRoute("default", "{controller=Home}/{action=Index}/{id?}");
});
app.MapWhen(x => !x.Request.Path.Value.StartsWith("/api",
StringComparison.OrdinalIgnoreCase), builder =>
{
builder.UseMvc(routes =>
{
routes.MapRoute("spa-fallback", "{*url}", new { controller = "Home", action =
"Index" });
});
});
This has been solved. I looked at the code above which was routed with the home controller. This led me to look at the HomeController. The difference between the applications is that the one that is not working has a MVC Route attribute with an added "/" in the Home controller.
[Route("/")] // remove this line
public class HomeController : Controller
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With