Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ASP.NET Core MVC Wildcard route not working with identical setup to another that is working

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 enter image description here

Startup.cs enter image description here

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 enter image description here

Web.config enter image description here

like image 821
Henry Ing-Simmons Avatar asked Aug 30 '18 11:08

Henry Ing-Simmons


1 Answers

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
like image 160
Justin Chew Avatar answered Oct 28 '22 16:10

Justin Chew