Going to talk more about the latest and the hot topic of the current trend in the market. It's an Angular2 and Asp.Net Core.
The Angular2 CLI makes it easy to create an Angular2 application else it's very complex configuration for developers.
Good news for those developers, who loved to work with asp.net using visual studio IDE because we will see how to work Angular2 application using visual studio IDE.
The first fall we will create an empty asp.net core solution.
We have created an empty solution so we need to add some packages to support MVC and static files.
Once above packages configured and restored completed than requiring to modified StartUp class to support configurations.
1. On ConfigureServices method add services.AddMvc();
2. On Configure method add following configurations.
app.UseMvc();
app.UseStaticFiles();
Also, add some redirect related configuraiton.
After including all, how startup class looking as below.
Now, Asp.Net core application is ready so we will create an angular application.
For the Angular application, we are going to create Class Library because it will show in visual studio.
Before we move ahead please visit angular cli website.
The first fall we are going to install npm angular cli globally.
After installing above npm, we are going to create Angular2 Application using the class library project.
so open command prompts and navigate to project\src\ClientApp folder and write the following command to create Angular2 Application.
It will take a while to create an application and install npm packages.
Once all set, run the application using ng serve so it will run on http://localhost:4200.
If you want to run the angular application from asp.net core application for that require the following configuration on angular-cli.json. file.
set up outDir path because its default is "dist" folder
Once you have done above configuration than build the angular2 application with the following command so it will build and copy the content of the angular2 application on mention output directory so you can directly run the asp.net core application with angular2.
Whenever you want to deploy this application then first required to execute above command and then public asp.net core application for the deployment.
For the source code, you can visit my Github repository.
Hope you like it!!
The Angular2 CLI makes it easy to create an Angular2 application else it's very complex configuration for developers.
Good news for those developers, who loved to work with asp.net using visual studio IDE because we will see how to work Angular2 application using visual studio IDE.
The first fall we will create an empty asp.net core solution.
We have created an empty solution so we need to add some packages to support MVC and static files.
"Microsoft.AspNetCore.Mvc": "1.0.1", "Microsoft.AspNetCore.StaticFiles": "1.0.0"
Once above packages configured and restored completed than requiring to modified StartUp class to support configurations.
1. On ConfigureServices method add services.AddMvc();
2. On Configure method add following configurations.
app.UseMvc();
app.UseStaticFiles();
Also, add some redirect related configuraiton.
After including all, how startup class looking as below.
public class Startup { public void ConfigureServices(IServiceCollection services) { services.AddMvc(); } public void Configure(IApplicationBuilder app, IHostingEnvironment env, ILoggerFactory loggerFactory) { loggerFactory.AddConsole(); if (env.IsDevelopment()) { app.UseDeveloperExceptionPage(); } app.Use(async (context, next) => { await next(); if (context.Response.StatusCode == 404 && !Path.HasExtension(context.Request.Path.Value) && !context.Request.Path.Value.StartsWith("/api/")) { context.Request.Path = "/index.html"; await next(); } }); app.UseMvc(); app.UseStaticFiles(); } }
Now, Asp.Net core application is ready so we will create an angular application.
For the Angular application, we are going to create Class Library because it will show in visual studio.
Before we move ahead please visit angular cli website.
The first fall we are going to install npm angular cli globally.
npm install -g angular-cli
After installing above npm, we are going to create Angular2 Application using the class library project.
so open command prompts and navigate to project\src\ClientApp folder and write the following command to create Angular2 Application.
ng new ClientApp
It will take a while to create an application and install npm packages.
Once all set, run the application using ng serve so it will run on http://localhost:4200.
If you want to run the angular application from asp.net core application for that require the following configuration on angular-cli.json. file.
set up outDir path because its default is "dist" folder
"outDir": "../../Angular2CliAspNetCore/wwwroot"
Once you have done above configuration than build the angular2 application with the following command so it will build and copy the content of the angular2 application on mention output directory so you can directly run the asp.net core application with angular2.
ng build
Whenever you want to deploy this application then first required to execute above command and then public asp.net core application for the deployment.
For the source code, you can visit my Github repository.
Hope you like it!!
0 comments:
Post a Comment