You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
This repository was archived by the owner on Apr 8, 2020. It is now read-only.
Repository navigation
This repository was archived by the owner on Apr 8, 2020. It is now read-only.
It's for when you want to handle the 404 within your front-end application (and as noted below, let all Routes go through so your front-end can handle them)
@MarkPieszak My use case - which I assume is very common - is to want to handle bad angular routes in the front-end (by either sending to a "404" routes or just loading a "home" route), but throw 404s on bad API calls (that is, requests whose URL follows a certain pattern eg. "/api").
You want to use a mapWhen looking for something that starts with /api, use this below and you'll be all set:
// this part you already haveapp.UseMvc(routes =>{routes.MapRoute(name:"default",template:"{controller=Home}/{action=Index}/{id?}");});// here you can see we make sure it doesn't start with /api, if it does, it'll 404 within .NET if it can't be foundapp.MapWhen(x =>!x.Request.Path.Value.StartsWith("/api"), builder =>{builder.UseMvc(routes =>{routes.MapSpaFallbackRoute(name:"spa-fallback",defaults:new{controller="Home",action="Index"});});});
Hope that helps!
Reacted by Michael Washington, im1dermike, Tobbe, Manas Talukdar, Terje Hoås, Zihong Chen, Giovanni Costagliola, Zandt Tittle, Adam, mikolajszczepanski and 24 more
Reacted by Sourav Kings, Michael Prüfer, Levi, Jamir Castro Araújo, Benjamin Pill, AlexDubr and Richard Banks
Thanks @MarkPieszak for the answer. If this becomes a common request, we could add a further excludePrefix option onto MapSpaFallbackRoute so you can set it to api.
Reacted by Mark Pieszak, Aaron Hoffman, Michael Schnerring, Marco Monducci, Michael Prüfer and khiemips
It's for when you want to handle the 404 within your front-end application.
That's not the whole story. It's mostly just so that all URLs that might be handled by your front-end application cause the front-end application to be returned. For example, a request for the URL /counter (in the default template) should cause the front-end application to be loaded, which will then display a suitable UI. If you didn't have MapSpaFallbackRoute then the server would just return a 404 for /counter since there's no matching MVC controller.
That's not the whole story. It's mostly just so that all URLs that might be handled by your front-end application cause the front-end application to be returned. For example, a request for the URL /counter (in the default template) should cause the front-end application to be loaded, which will then display a suitable UI. If you didn't have MapSpaFallbackRoute then the server would just return a 404 for /counter since there's no matching MVC controller.
For anyone who wants to use ASP.NET Razor Pages that came out with .NET Core 2.0 I never could get map fallback route to work so I pivoted and went a different direction which implements @MarkPieszak 's solution of only routing all requests to the SPA when the route doesn't start with API.
@buvinghausen That works well. Just be aware that every request that doesn't match a static file will get routed to your SPA - even requests with file extensions. MapSpaFallbackRoute does not match requests that look like filenames - that is, have an extension at the end.
Edit: I just realize you do account for periods in the request - but I'll leave my comment here just in case it helps others. Thanks!
Activity
MarkPieszak commentedon May 24, 2017
It's for when you want to handle the 404 within your front-end application (and as noted below, let all Routes go through so your front-end can handle them)
im1dermike commentedon May 24, 2017
@MarkPieszak My use case - which I assume is very common - is to want to handle bad angular routes in the front-end (by either sending to a "404" routes or just loading a "home" route), but throw 404s on bad API calls (that is, requests whose URL follows a certain pattern eg. "/api").
Can you give me a recommendation?
MarkPieszak commentedon May 24, 2017
You want to use a mapWhen looking for something that starts with
/api, use this below and you'll be all set:Hope that helps!
SteveSandersonMS commentedon May 24, 2017
Thanks @MarkPieszak for the answer. If this becomes a common request, we could add a further
excludePrefixoption ontoMapSpaFallbackRouteso you can set it toapi.SteveSandersonMS commentedon May 24, 2017
Also:
That's not the whole story. It's mostly just so that all URLs that might be handled by your front-end application cause the front-end application to be returned. For example, a request for the URL
/counter(in the default template) should cause the front-end application to be loaded, which will then display a suitable UI. If you didn't haveMapSpaFallbackRoutethen the server would just return a 404 for/countersince there's no matching MVC controller.im1dermike commentedon May 24, 2017
@MarkPieszak Perfect. Your solution works as desired.
Fantastic responsiveness as usual.
ADefWebserver commentedon May 24, 2017
So umm yeah, this is REALLY an important point that I hope does not get lost :)
Meaning. I can send someone a url like this:
http://helloworlddata.azurewebsites.net/counter
..and it works even though there is really no
/counterpage. It exists only as an Angular route :)MarkPieszak commentedon May 24, 2017
Yes apologies I should of mentioned that! @SteveSandersonMS
buvinghausen commentedon Sep 9, 2017
For anyone who wants to use ASP.NET Razor Pages that came out with .NET Core 2.0 I never could get map fallback route to work so I pivoted and went a different direction which implements @MarkPieszak 's solution of only routing all requests to the SPA when the route doesn't start with API.
pholly commentedon Oct 25, 2017
@buvinghausen That works well. Just be aware that every request that doesn't match a static file will get routed to your SPA - even requests with file extensions.
MapSpaFallbackRoutedoes not match requests that look like filenames - that is, have an extension at the end.Edit: I just realize you do account for periods in the request - but I'll leave my comment here just in case it helps others. Thanks!
Edit2: Just opened an issue with https://github.com/aspnet/JavaScriptServices with a suggestion: #1354
ripasfilqadar commentedon Apr 5, 2018
is this the correct way to using controller from some areas?