简体   繁体   English

服务Angular spa打破根

[英]Serve Angular spa pathed off the root

I'm looking to upgrade an existing asp.net 4.5 mvc site which has two angular applications to an asp.netcore 2 mvc site with potentially two spa's. 我正在寻找升级现有的asp.net 4.5 mvc网站,它有两个角度应用程序到asp.netcore 2 mvc网站,可能有两个spa。 using Aspnet Javascriptservices with the new angular cli template. 使用Aspnet Javascriptservices和新的角度cli模板。

Ideally i want to host two spa's one at http://mysite/member and the other at http://mysite/admin 理想情况下,我想在http://mysite/member主持两个spa,另一个在http://mysite/admin

I've started with just one members and i initially used <base href="/members/" /> in the index.html (but then swapped to use the baseHref" property in the.angular-cli.json (giving the same results)) which half works. Locally when debugging the app it serves pages and navigates as expected but in the console i can see zone.js errors. 我从一个members开始,我最初在index.html中使用<base href="/members/" /> (但随后在.angular-cli.json中交换使用baseHref“属性(给出相同的)结果))一半工作。在调试应用程序时,它本地提供页面并按预期导航,但在控制台中我可以看到zone.js错误。

在此输入图像描述

If i open a new tab and paste in 如果我打开一个新标签并粘贴

http://localhost:50930/**members**/sockjs-node/info?t=1518084503138

then i get what looks like a correct response 然后我得到了正确的回应

{"websocket":true,"origins":["*:*"],"cookie_needed":false,"entropy":2082738399}

If i deploy this to Azure app service (production) then navigating to 如果我将此部署到Azure应用服务(生产),然后导航到

https://mysite.azurewebsites.net/members then the spa doesn't load at all and it seems that the bundled js has the index.html which is loading it inside. https://mysite.azurewebsites.net/members然后spa根本没有加载,似乎捆绑的js有index.html,它正在加载它。 在此输入图像描述

Has anyone managed to use the angular spa template as an application served off the route of an MVC site? 有没有人设法使用角度spa模板作为MVC网站路线下的应用程序? I created a ticket on the repo but i suspect its beyond the scope of the project https://github.com/aspnet/JavaScriptServices/issues/1518 我在回购中创建了一张票,但我怀疑它超出了项目的范围https://github.com/aspnet/JavaScriptServices/issues/1518

Also created a repo to demonstrate what i am trying to achieve https://github.com/tbertenshaw/MultiSpa 还创建了一个回购来展示我想要实现的目标https://github.com/tbertenshaw/MultiSpa

You have to configure the webserver to always return the index.html page of your angular pwa. 您必须将网络服务器配置为始终返回角度pwa的index.html页面。

Default, the server returns an error 404, because for /members there is no file present. 默认情况下,服务器返回错误404,因为/ members没有文件存在。 But you want angular to handle the routes and therefore you have to always serve the index.html instead of an 404 error page. 但是你想要angular处理路由,因此你必须始终提供index.html而不是404错误页面。

This documentation explains this further explicitly for the aspnet JavaScriptServices: https://github.com/aspnet/JavaScriptServices/tree/dev/src/Microsoft.AspNetCore.SpaServices#routing-helper-mapspafallbackroute 本文档对aspnet JavaScriptServices进一步明确解释了这一点: https//github.com/aspnet/JavaScriptServices/tree/dev/src/Microsoft.AspNetCore.SpaServices#routing-helper-mapspafallbackroute

I have used this before in my apps. 我以前在我的应用程序中使用过这个。 In a later version I added caching to make it faster, but it always works regardless of where it's deployed. 在更高版本中,我添加了缓存以使其更快,但无论在何处部署,它始终都能正常工作。 It makes moving files between servers or even just folders very easy, especially when we move between dev, test, and prod servers. 它使得在服务器甚至文件夹之间移动文件非常容易,特别是当我们在dev,test和prod服务器之间移动时。

using System;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Http;

namespace Management.Middleware
{
    public class AngularDefaultRouteMiddleware
    {
        private readonly RequestDelegate _next;

        public AngularDefaultRouteMiddleware(RequestDelegate next)
        {
            _next = next;
        }

        public async Task Invoke(HttpContext context)
        {
            if (context.Request.Path == "/")
            {
                await context.Response.WriteAsync(await GetAngularSpa(context.Request.PathBase));
            }
            else
            {
                await _next(context);
                if (context.Response.StatusCode == 404 && !context.Response.HasStarted)
                {
                    await context.Response.WriteAsync(await GetAngularSpa(context.Request.PathBase));
                }
            }
        }

        private async Task<string> GetAngularSpa(string pathBase)
        {
            string html = await System.IO.File.ReadAllTextAsync($"{Environment.CurrentDirectory}\\wwwroot\\index.html");
            return html.Replace("<base href='/'>", $"<base href='{pathBase}'>");
        }
    }

    public static class AngularDefaultRouteMiddlewareExtensions
    {
        public static IApplicationBuilder UseAngularDefaultRoute(this IApplicationBuilder app)
        {
            return app.UseMiddleware<AngularDefaultRouteMiddleware>();
        }
    }
}

Then you can just call this middleware from Startup.cs with app.UseAngularDefaultRoute(); 然后你可以使用app.UseAngularDefaultRoute();Startup.cs调用这个中间件app.UseAngularDefaultRoute();

Tim, did you ever get this working? 蒂姆,你有没有得到这个工作? Im currently trying to get an Aurelia app hosted from a sub-path within a .NET Core MVC app. 我目前正在尝试从.NET Core MVC应用程序中的子路径托管Aurelia应用程序。 I am almost there, the main difficulty has been in creating an MVC and Webpack config that works in both production and development. 我几乎就在那里,主要的困难在于创建一个适用于生产和开发的MVC和Webpack配置。

My sample project is based off the new 2.1 style Angular SPA template which uses the 'UseAngularCliServer' or 'UseProxyToSpaDevelopmentServer' middleware when in dev mode and serves the bundled files from the 'dist' dir when in production mode. 我的示例项目基于新的2.1样式Angular SPA模板,该模板在开发模式下使用'UseAngularCliServer''UseProxyToSpaDevelopmentServer'中间件,并在生产模式下从'dist'目录提供捆绑文件。

UPDATE: I have published a working repo for this scenario, no sockjs-node errors, also works with HMR, please see the thread here: https://github.com/aspnet/JavaScriptServices/issues/1518 更新:我已经为这个场景发布了一个工作回购,没有sockjs-node错误,也适用于HMR,请看这里的主题: https//github.com/aspnet/JavaScriptServices/issues/1518

It works in both Development and Production modes. 它适用于开发和生产模式。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM