Add Angular 4 to an ASP.NETCore Project

隐身守侯 提交于 2019-11-29 07:06:32

[i am aware you didn't ask this specifically but just wanted to share a thought which is very relevant in my opinion]

I was at the exactly the same point a few months ago and i decided going with angular cli. and i thank God i made that decision. Uncomparably more clear and it actually put things how they should be: .net core web api backend and completely separated angular client. just exactly the same as you wouldn't put your iphone/android app into VS solution, there is no real reason to put angular one neither.

UPDATE Either use MVC with Razor, OR use Angular as a SPA, and if you use Angular, use the Angular CLI. And if you use angular, feed data into it with asp.net (core) Web Api as a REST backend

I can see how this is confusing, but dee zg is correct. You're going to want two separate projects. One will be the .net core backend api (REST) and the other will be the Angular 2 spa. There will be a complete separation.

With AngularJS, you could just drop in the js file into a razor view and call it a day. However, with Angular 2/4 and React, it's best to host each project in it's own solution. This might seem silly at the beginning, but when you build out your api, it can be easily future proofed by having the capability of also being consumed by mobile apps, desktop apps, etc.

I know there is a dotnet spa services angular cli powershell command for .net core, but it is really kind of a mess and actually outputs invalid html that could possibly be detrimental to SEO, etc. It's best, IMO, to just use the angular CLI in a separate project.

When it comes to production, you'll host the .net core api and the angular 2 projects separately.

Hope that helps.

Anton

It’s not totally clear to me what you mean by "Add Angular 4 to an ASP.NET Core Project", but one way to do this is the following:

  1. Create an Angular 4 project (using angular-cli).
  2. Create an ASP.NET Core project (using .NET Core CLI) in the same directory as the Angular project.
  3. Adjust the webpack.config.js file (which is part of the angular-cli installation) in order to automatically put bundled files in the wwwroot directory (webpack will be responsible for this).

On my blog, I have a detailed tutorial demonstrating this approach.

I have been working on a Angular 4(SPA) and .Net core web api. You can check out the repository and you might get a better understanding.

I had to find solutions to many areas like authentication, authorisation, social login, MongoDB connector e.t.c

Hope this would save time for someone stumbling on this.

https://github.com/Sathya-B/Angular-.NetCore-MongoDB

Judyll

I am using Angular 6 and ASP.NET CORE 2.1 but this might work on Angular 4.

CREATE A NEW ANGULAR PROJECT IN VS2017:

  1. Create a new Web App in VS. The folder name should not contain '.' since Angular will not accept it. For example: DatingApp-SPA. And then select 'Blank'.
  2. Close VS so that it will not lock the folder for any permission
  3. Navigate to the base path of the project folder.
  4. Run 'ng new DatingApp-SPA'. This will add Angular goodies to the existing DatingApp-SPA project.
  5. Open the solution again on VS.

CREATE A NEW COMPONENT:

  1. Open command prompt and navigate to the project folder
  2. Run the command:

    ng generate component component-name-with-lowercase

This will generate the .html, .spec.ts, .ts, .css files and update the app.module.ts

CREATE A NEW SERVICE:

  1. Open command prompt and navigate to the project folder
  2. Run the command:

    ng generate service _component-name-with-lowercase_
    

This will generate the .ts and .spec.ts by default in the \app folder. We can move these files on another folder like \app\_services

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!