I have a ASP.NET Core 2.2 application with Vue.js for my frontend. In my startup I have the following routes:
app.UseMvc(routes =>
{
routes.MapRoute(
name: "mvc",
template: "{controller=Home}/{action=Index}/{id?}");
// TODO: When api route doesn't exists it goes into the spa-fallkback route. We need to prevent this.
routes.MapSpaFallbackRoute(
name: "spa-fallback",
defaults: new { controller = "Home", action = "Index" });
});
The problem is with my API routes. When the API URL exists we have no problem, but when the URL doesn't exist, it goes to the spa-fallback routes.
Is there a way to set up the routes so that when the URL starts with /api/ and no route is found, it returns a 404 instead of the spa-fallback?
You can always create a catch-all action manually:
public class HomeController : Controller
{
// SPA Fallback
public IActionResult Index()
{
return View();
}
// Disable all other /api/* routes.
[Route("/api/{**rest}")]
public IActionResult Api()
{
return NotFound("");
}
}
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