简体   繁体   English

部署Angular 6 ASP.NET Core应用程序

[英]Deploying Angular 6 ASP.NET Core application

I have developed an asp.net core 2.0 MVC application with the addition of an Angular 6 frontend application. 我开发了一个asp.net core 2.0 MVC应用程序,增加了一个Angular 6前端应用程序。 They both exist in the same project structure. 它们都存在于同一个项目结构中。 The asp.net core application acts as an API for the client side Angular 6 application. asp.net核心应用程序充当客户端Angular 6应用程序的API。

I have been developing them side-by-side and included the following code segments inside the Startup.cs file to allow development whilst both applications are running: 我一直在开发它们,并在Startup.cs文件中包含以下代码段,以便在两个应用程序运行时进行开发:

ConfigureServices
      services.AddSpaStaticFiles(configuration => {
        configuration.RootPath = "ClientApp/dist";
      });

Configure
      app.UseSpa(spa => {
        spa.Options.SourcePath = "ClientApp";

        if (env.IsDevelopment()) {
          spa.UseAngularCliServer(npmScript: "start");
        }
      });

The project structure for the Angular application can be found in ClientApp . 可以在ClientApp中找到Angular应用程序的项目结构。

I know wish to deploy this project to IIS. 我知道希望将此项目部署到IIS。 So I am moving away from a development environment so I know that the line of code: spa.UseAngularCliServer(npmScript: "start"); 所以我正在离开开发环境,所以我知道代码行: spa.UseAngularCliServer(npmScript: "start"); is not going to be run. 不会被运行。

When I publish the project through Visual Studio and move it to the inetpub folder as I would do for other applications, it does not serve the pages that I have developed within the Angular 6 application. 当我通过Visual Studio发布项目并将其移动到inetpub文件夹时,就像我为其他应用程序所做的那样,它不会为我在Angular 6应用程序中开发的页面提供服务。 I get 500 internal server error when trying to access a page such as /Home that I have defined within the RoutingModule of the Angular 6 application. 尝试访问我在Angular 6应用程序的RoutingModule中定义的/ Home这样的页面时出现500内部服务器错误。

I am presuming that it is because I need to build the Angular application (which I can do through ng build --prod ) and add the compiled JS files to a HTML page. 我认为这是因为我需要构建Angular应用程序(我可以通过ng build --prod )并将已编译的JS文件添加到HTML页面。 But I am unsure how to go about this. 但我不确定如何解决这个问题。 If anyone has any links to relevant webpages that would be greatly appreciated. 如果有人有任何链接到相关的网页,将不胜感激。 Or if you can provide any insight that would be very helpful. 或者,如果您能提供任何非常有用的见解。

Update #1: 更新#1:

Within the Startup.cs file, I made app.UseDeveloperExceptionPage() available for when running in production mode. 在Startup.cs文件中,我使app.UseDeveloperExceptionPage()在生产模式下运行时可用。

Instead of a 500 internal server error page, I got exception: The SPA default page middleware could not return the default page '/index.html' because it was not found, and no other middleware handled the request. 而不是500内部服务器错误页面,我得到了例外: SPA默认页面中间件无法返回默认页面'/index.html',因为找不到它,并且没有其他中间件处理该请求。

I also noticed that the ClientApp/dist folder was not present after publishing. 我还注意到发布后ClientApp / dist文件夹不存在。 I manually built ClientApp and added it to the application in inetpub. 我手动构建ClientApp并将其添加到inetpub中的应用程序。 Now, I get the error in the console: 现在,我在控制台中收到错误:

runtime.a66f828dca56eeb90e02.js:1 Uncaught SyntaxError: Unexpected token <

polyfills.7a0e6866a34e280f48e7.js:1 Uncaught SyntaxError: Unexpected token <

Request:10 Resource interpreted as Stylesheet but transferred with MIME type text/html: "http://localhost:8080/styles.169e0a841442606822c8.css".

scripts.ee7fed27c36eaa5fa8a9.js:1 Uncaught SyntaxError: Unexpected token <

main.befe6f4d3c1275f2e1b3.js:1 Uncaught SyntaxError: Unexpected token <

You need to use this code for ASP.NET Core 2.0 , not what you are using. 您需要将此代码用于ASP.NET Core 2.0 ,而不是您正在使用的代码。 My application is published using this approach. 我的应用程序使用这种方法发布。

        app.UseMvc(routes =>
        {
 //Remove this if you don't need it
            routes.MapRoute(
                name: "default",
                template: "{controller=Home}/{action=Index}/{id?}");

            routes.MapSpaFallbackRoute(
                name: "spa-fallback",
              defaults: new { controller = "Home", action = "SPAIndex" }); // For SPA 
        });

You need to have a Action in your HomeController which returns a View. 你需要在HomeController中有一个Action,它返回一个View。 Code for View is 代码是

<app asp-prerender-module="ClientApp/dist/main-server">Loading...</app>

Visual Studio 2019 brings a project template for this, just select new project in first steps on start page, ASP.NET Core Web Application C#, enter a solution name, and select Angular project template. Visual Studio 2019为此提供了一个项目模板,只需在开始页面的第一步中选择新项目,ASP.NET Core Web Application C#,输入解决方案名称,然后选择Angular项目模板。 You may base your solution on ASP.NET Core running under .NET Core or .NET Framework. 您可以将解决方案基于在.NET Core或.NET Framework下运行的ASP.NET Core。

Just make this change, it should work for Prod IIS 只需进行此更改,它应该适用于Prod IIS

 public void ConfigureServices(IServiceCollection services)
    {
        services.AddMvc().SetCompatibilityVersion(CompatibilityVersion.Version_2_2);

        // In production, the Angular files will be served from this directory
        services.AddSpaStaticFiles(configuration =>
        {
            configuration.RootPath = "ClientApp/dist/client";

        });
    }

Here is some stop you can follow : 这是您可以遵循的一些停止:

1- make sure you published your application currently see the stop in this link publishing your dotcore application with angular template 1-确保您已发布您的应用程序当前在此链接中看到停止使用角度模板发布您的dotcore应用程序

2- in your publish folder run the exe application with your project name on it. 2-在您的发布文件夹中运行带有项目名称的exe应用程序。 if the application process successfully then issue is in your iis 如果申请流程成功,那么问题就在您的iis

make a request from iis and go to event viewer iis提出请求并转到事件查看器

windows logs > Application Windows日志>应用程序

if your see some error click on it and see the details 如果您看到一些错误,请单击它并查看详细信息

if the issue is related with can't run or create: 如果问题与无法运行或创建有关:

go to your iis pool and advanced settings and change identity to local system 转到您的iis池和高级设置,并将身份更改为本地系统

and you other issue posted here 而你在这里发布的其他问题

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

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