Host spa(s) from paths off MVC app #1518
Comments
Please see docs about ASP.NET middleware, as it allows you to set up configurations like this. For example, |
@SteveSandersonMS That works great, only thing is when i navigate to the relative path #startup.cs public void Configure(IApplicationBuilder app, IHostingEnvironment env)
{
if (env.IsDevelopment())
{
app.UseBrowserLink();
app.UseDeveloperExceptionPage();
app.UseDatabaseErrorPage();
}
else
{
app.UseExceptionHandler("/Error");
}
app.UseStaticFiles();
app.UseSpaStaticFiles();
app.UseAuthentication();
app.UseMvc(routes =>
{
routes.MapRoute(
name: "default",
template: "{controller}/{action=Index}/{id?}");
});
app.Map(new PathString("/memberspa"), appMember =>
{
appMember .UseSpa(spa =>
{
spa.Options.SourcePath = "ClientApp";
if (env.IsDevelopment())
{
spa.UseAngularCliServer(npmScript: "start");
}
});
});
} |
Getting the client-side code to load the correct files is more of a question about your SPA framework. It looks like you're using Angular, so Angular docs would be the place to go. I think they make use of a |
Yes its the vanilla angular template. I already modified the base tag prior to the app.Map change I'll see if i can find more details. |
ok
|
created a repo to illustrate what i have done and to replicate the errors https://github.com/tbertenshaw/MultiSpa |
Any of you help me to sort it. Like previous version we load angular components from .cshtml for new angular 5 template , I'm unable make it possible. How could I achieve it. if not suggest good process. |
@tbertenshaw I made the path work by simply doing the following:
AND
So my sample is running http://localhost:5000/spa |
@kdcllc are you using the example repo? I've just checked and still getting the zonejs errors. Invested a heap of time trying to get this working. Even opened a bounty on stackoverflow and didn't get a resolution. |
@tbertenshaw Any updates to this? Is this working? Having a typical MVC site with a /spa section that is authorized is awesome. |
@Vx2gas nah couldn't get the issues i spotted resolved, and think the owners are treating this issue as closed and thus not seeing the comments. |
Thanks @tbertenshaw. @kdcllc anychance you can fork the repo to show us its working? |
@Vx2gas and @tbertenshaw I have created a git repo here https://github.com/kdcllc/aspnetcore-angular-realworld-example-app. Please take a look at the code that makes use of /spa url segment. |
@tbertenshaw I'm not sure what specific errors you are getting. When I pulled your repo down, everything seems to work just fine. If I simply add the following, I can protect the members area inside of
|
@tbertenshaw I would add, I can "hack" my way to solve the problem by doing the following: |
@tbertenshaw Here is the repo that I forked with it working. I removed all the testing stuff for simplicity to get to the bare bones. It also been updated to angular 6 etc... @SteveSandersonMS I know you mentioned its an angular-cli issue... can you please double check with my repo and see why I have to do the I just don't know enough on what is going on. |
@tbertenshaw Did you ever get this working? It's easy enough to get this working in production mode where static files are served from 'ClientApp/dist' but to also get it to work properly in dev mode using SpaProxy requires a bit of trickery. To resolve the request errors for '/sockjs_node/' you need to capture those requests outside of app.Map("path"...) and forward them to the SpaProxy. (i believe that port 4200 needs to be used for the Angular Spa template in place of 8080 below) This is what i had to do in the middleware before app.Map("path"...), in my case i have my Spa running under the /app path.
I will post a link to a working repo soon enough, it will be for an Aurelia app but it should also work fine for Angular as long as Angular is also configured to build and run for a matching sub-path. |
@tbertenshaw It is possible the change the websockets url which is used by the webpack-dev-server so that requests for /sockjs-node/ are made from a different base url, such as /app/sockjs-node/ but as far as i know this would require the Angular webpack config the be modified. To do that you would need to eject from the Angular CLI by running Here is my working repo which has Angular running from an /app/ sub-path off root: https://github.com/chrisckc/DotNetCoreAngularHMR-Spa/tree/spa-served-from-sub-path Checkout the 'spa-served-from-sub-path' branch. It works in both Development and Production modes. Here is the link to the stackoverflow thread referenced previously. |
Is it possible to define the spa routes as paths off an existing mvc site?
I.e. I want all normal defined mvc routing to work as expected, however if the relative path off the root is
<base href="/memberspa">
for example (defined in the index.html) then i want that to serve my spa. Currently it seems that if i hit a route that exists by a existing mvc controller then that it is served by the controller. But if i pass any route which doesn't exist it loads my spa. i.e./thisdoesnotexist
redirects to/memberspa
. But /home (which is served by my home controller) correctly is rendered by the home/index method.If this is possible then i would ideally want to have two separate spa's served from paths off the sites root. The other potentially being
/admin
.I'm currently using the following in my startup.cs
The text was updated successfully, but these errors were encountered: