Different best-practices advise us to split the application into two separate projects , which in our case is the .NET Core Web API and Angular projects. The main advantages of this approach will be the following:
- Rigid connection of the web interface with the server side
- Strongly complicated minimally working version of the application
- Lack of ability to use Angular CLI
- Extra pre-installed packages
- Breaking Some Principles from the Angular Style Guide
In this scenario, there are two final scenarios for production:
- Two independent projects, which will allow us to further implement an alternative interface, without touching the project with the server part
- Narrowed global search scope , which allows you to search more efficiently and easily
- Abstraction from the working environment in which the server part is developed, Visual Studio for example - we can use VS Code, Sublime Text, Atom or another editor that is convenient for you
My task was just the second scenario , so it was more preferable for economic reasons. And so, when I tried to figure out how to make friends with the .NET Core Web API project with the Angular project, so that during development we had two separate projects, and in production - only one , and specifically. NET Core web site, I could not find a full-fledged guide "from scratch to a working application." It was necessary to collect piecemeal solutions from English-speaking forums and blogs. If you suddenly had the same task, then it will be enough to read my article.
- You host the web interface at one address, and the server at another.
- Or you collect projects in one magic way and host only him
If you already have Visual Studio 2017 installed and during installation you chose .NET Core Development , then you already have the .NET Core SDK and do not need to install it. However, Node.js will have to be installed separately even if Node.js Development was selected. Npm will install with Node.js. Angular CLI is installed globally from the command line via npm (the instruction is from the link above).
- .NET Core SDK - version 2.0 or higher
- Node.js - version 8.9.0 or higher
- npm - version 5.5.0 or higher
- Angular CLI - 1.6.5 version or higher
- Visual studio code
dotnet --version # .NET Core node --version # Node.js npm --version # npm ng --version # Angular CLI
dotnet new webapi -n Project.WebApi
dotnet run
cd ..\ ng new Project.Angular
cd ./Project.Angular ng serve --open
{ "/api/*": { "target": "http://localhost:5000/", "secure": false, "logLevel": "debug" } }
{ "/api/*": { "target": "http://localhost:5000/", "secure": false, "logLevel": "debug" } }
{ ... scripts: { ... "start": "ng serve --proxy-config proxy.config.json", } }
{ { "name": "project.angular", "version": "0.0.0", "license": "MIT", "scripts": { "ng": "ng", "start": "ng serve --proxy-config proxy.config.json", "build": "ng build --prod", "test": "ng test", "lint": "ng lint", "e2e": "ng e2e" }, "private": true, "dependencies": { "@angular/animations": "^5.2.0", "@angular/common": "^5.2.0", "@angular/compiler": "^5.2.0", "@angular/core": "^5.2.0", "@angular/forms": "^5.2.0", "@angular/http": "^5.2.0", "@angular/platform-browser": "^5.2.0", "@angular/platform-browser-dynamic": "^5.2.0", "@angular/router": "^5.2.0", "core-js": "^2.4.1", "rxjs": "^5.5.6", "zone.js": "^0.8.19" }, "devDependencies": { "@angular/cli": "1.6.7", "@angular/compiler-cli": "^5.2.0", "@angular/language-service": "^5.2.0", "@types/jasmine": "~2.8.3", "@types/jasminewd2": "~2.0.2", "@types/node": "~6.0.60", "codelyzer": "^4.0.1", "jasmine-core": "~2.8.0", "jasmine-spec-reporter": "~4.2.1", "karma": "~2.0.0", "karma-chrome-launcher": "~2.2.0", "karma-coverage-istanbul-reporter": "^1.2.1", "karma-jasmine": "~1.1.0", "karma-jasmine-html-reporter": "^0.2.2", "protractor": "~5.1.2", "ts-node": "~4.1.0", "tslint": "~5.9.1", "typescript": "~2.5.3" } } }
{ ... scripts: { ... "build": "ng build --prod --output-path ../Project.WebApi/wwwroot", } }
{ { "name": "project.angular", "version": "0.0.0", "license": "MIT", "scripts": { "ng": "ng", "start": "ng serve --proxy-config proxy.config.json", "build": "ng build --prod --output-path ../Project.WebApi/wwwroot", "test": "ng test", "lint": "ng lint", "e2e": "ng e2e" }, "private": true, "dependencies": { "@angular/animations": "^5.2.0", "@angular/common": "^5.2.0", "@angular/compiler": "^5.2.0", "@angular/core": "^5.2.0", "@angular/forms": "^5.2.0", "@angular/http": "^5.2.0", "@angular/platform-browser": "^5.2.0", "@angular/platform-browser-dynamic": "^5.2.0", "@angular/router": "^5.2.0", "core-js": "^2.4.1", "rxjs": "^5.5.6", "zone.js": "^0.8.19" }, "devDependencies": { "@angular/cli": "1.6.7", "@angular/compiler-cli": "^5.2.0", "@angular/language-service": "^5.2.0", "@types/jasmine": "~2.8.3", "@types/jasminewd2": "~2.0.2", "@types/node": "~6.0.60", "codelyzer": "^4.0.1", "jasmine-core": "~2.8.0", "jasmine-spec-reporter": "~4.2.1", "karma": "~2.0.0", "karma-chrome-launcher": "~2.2.0", "karma-coverage-istanbul-reporter": "^1.2.1", "karma-jasmine": "~1.1.0", "karma-jasmine-html-reporter": "^0.2.2", "protractor": "~5.1.2", "ts-node": "~4.1.0", "tslint": "~5.9.1", "typescript": "~2.5.3" } } }
app.UseDefaultFiles(); app.UseStaticFiles();
public void Configure(IApplicationBuilder app, IHostingEnvironment env) { if (env.IsDevelopment()) { app.UseDeveloperExceptionPage(); } app.UseDefaultFiles(); app.UseStaticFiles(); app.UseMvc(); }
app.UseCors(builder => builder.WithOrigins("http://localhost:4200"));
public void Configure(IApplicationBuilder app, IHostingEnvironment env) { if (env.IsDevelopment()) { app.UseDeveloperExceptionPage(); } app.UseDefaultFiles(); app.UseStaticFiles(); app.UseCors(builder => builder.WithOrigins("http://localhost:4200")); app.UseMvc(); }
services.AddCors();
public void ConfigureServices(IServiceCollection services) { services.AddCors(); services.AddMvc(); }
using System; using System.Collections.Generic; using System.Linq; using System.Threading.Tasks; using Microsoft.AspNetCore.Builder; using Microsoft.AspNetCore.Hosting; using Microsoft.Extensions.Configuration; using Microsoft.Extensions.DependencyInjection; using Microsoft.Extensions.Logging; using Microsoft.Extensions.Options; namespace Project.WebApi { public class Startup { public Startup(IConfiguration configuration) { Configuration = configuration; } public IConfiguration Configuration { get; } public void ConfigureServices(IServiceCollection services) { services.AddMvc(); services.AddCors(); // <-- } public void Configure(IApplicationBuilder app, IHostingEnvironment env) { if (env.IsDevelopment()) { app.UseDeveloperExceptionPage(); } app.UseDefaultFiles(); // <-- app.UseStaticFiles(); // <-- app.UseCors(builder => builder.WithOrigins("http://localhost:4200")); // <-- :) app.UseMvc(); } } }
Source: https://habr.com/ru/post/349522/
All Articles