Javascriptservices: Asp.net Core 2 Razor Pages MapSpaFallback suggestion

Created on 25 Oct 2017  路  9Comments  路  Source: aspnet/JavaScriptServices

MapSpaFallback works well for MVC routes but does not exist for the new Razor Pages.

If SpaRouteConstraint were public instead of internal with a little work developers can add the constraint to Razor pages:

C# //Starup.cs ConfigureServices //thank you Scott Hanselman https://www.hanselman.com/blog/AddingACustomInlineRouteConstraintInASPNETCore10.aspx services.Configure<RouteOptions>(options => options.ConstraintMap.Add("spaRoute", typeof(SpaRouteConstraint))); services.AddMvc() .AddRazorPagesOptions(options => { options.Conventions.AddPageRoute("/Index", "{*catchAll:spaRoute(catchAll)}"); });

Would be even better if JavaScriptServices somehow added SpaRouteConstraint to the ConstraintMap.

For now I'll just copy the SpaRouteConstraint class.
Thanks,
Philip

Most helpful comment

Thanks for the suggestion. In the next update, we're providing an alternative to MapSpaFallbackRoute that isn't coupled to the concept of MVC actions. It looks like this:

app.UseSpa("root-directory-goes-here", () => { /* config goes here */ });

This is generic middleware that you can use either with MVC or Razor projects. Hopefully that will cover your requirements even more cleanly.

All 9 comments

I have just been using a regex to exclude the api routes.....

services
    .AddMvc()
    .AddRazorPagesOptions(options =>
    {
        options.Conventions.AddPageRoute("/Index", "{*url:regex(^(?!api).*$)}");
    });

Thanks for the suggestion. In the next update, we're providing an alternative to MapSpaFallbackRoute that isn't coupled to the concept of MVC actions. It looks like this:

app.UseSpa("root-directory-goes-here", () => { /* config goes here */ });

This is generic middleware that you can use either with MVC or Razor projects. Hopefully that will cover your requirements even more cleanly.

@SteveSandersonMS That'd be awesome. On at least two of my projects, there's just a thin layer of razor over an index.html (generated by webpack).

@buvinghausen thank you for your solution. I took me a long time to understand it but it works now.

@SteveSanderson: when is this app.UseSpa avalible. With the newset version of .Net Core I don't have it yet :(

@SteveSandersonMS also facing this issue still - is this an update we can expect soon?

@jjgriff93 We shipped UseSpa in ASP.NET Core 2.1.

Hi , @jjgriff93
I am testing https://github.com/vasyliv/angular6/tree/angular-server to include it my project. I cannot get spa fall back to work any suggestion?

Thanks @SteveSandersonMS - turns out it was a completely unrelated problem to spaservices! It was an SSL error. @jfborie I'm probably not best placed to help as I'm fairly new with the framework myself; however if you post your issue up separately and reference it I'm happy to take a look and see if i have any suggestions. Make sure you tell us what the error is with any code you can share

I hit too many snag i am trying with https://github.com/angular/universal-starter for now. Thx.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

raberana picture raberana  路  3Comments

caesay picture caesay  路  3Comments

natemcmaster picture natemcmaster  路  4Comments

DanHarman picture DanHarman  路  4Comments

tmedanovic picture tmedanovic  路  4Comments