Tuesday, July 2, 2019

ASP.NET Core What is it ?


ASP.Net Core is the latest trend of building modern, cloud-based, Internet-connected applications which introduce with .NET Framework 4.6. on words. and its a light-weight, open-source and cross-platform and it able to runs on the top of .NET Core or full .NET Framework having the version higher than 4.6.

Advantages of using Asp.Net Core
  • Open source
  • Cross platform framework which runs on macOS, Linux, and Windows.
  • Runs on the top of .NET Core or full .NET Framework having version 4.6+.
  • A unified framework for building web UI and web APIs.
  • Built-in dependency injection.
  • New light-weight and modular HTTP request pipeline.
  • Ships entirely as NuGet packages.
  • Side-by-side versioning when targeting .NET Core.
  • Built-in supports for SPA using client-side frameworks like Angular, React, Vue etc.
  • Supports IIS, Nginx, Apache, Docker, or self-host deployment.
 The Architecture of ASP.Net Core


ASP.NET Core has a number of architectural changes that result in a much leaner and modular framework. ASP.NET Core is no longer based on System.Web.dll. It is based on a set of granular and well factored NuGet packages. This allows you to optimize your app to include just the NuGet packages you need. The benefits of a smaller app surface area include tighter security, reduced servicing, improved performance, and decreased costs in a pay-for-what-you-use mod


Please note that ASP.NET Core and .NET Core are not the same. They are different, just like ASP.NET and .NET Framework are different.

Lets start the sample project 

here i am using VS 2017.

select the mvc template to follow the mvc architecture.


here is the structure of the project 
in the solution explorer the dependencies are the project references and it has two parts Nuget and SDK. since ASP.NET Core is not based on System.Web.dll its used NuGet packege. and We can see the SDK and inside SDK, we can see the “Microsoft.NETCore.App”. It is a target to .NET Core framework.
 Program.cs class
Program class is the main part of the ASP.NET Core. It contains static method and also beginning pointing of the application. This main method creates a host, builds a host, and runs the host.
 Startup Class
Startup class is a special class and it does not have any inheritance, interface, and over loading. It is mainly used to start the application
Time to say Hello world !
Add controller to project right click on controllers folder in solution explorer and Add -> Controller and name it as HelloWorldController.cs after

using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Mvc;

namespace MyCoreApp1.Controllers
{
    public class HelloWorldController : Controller
    {
        //public IActionResult Index()
        //{
        //    return View();
        //}
        public string Index()
        {
            return "This is the index action";
        }

        public string Welcome()
        {
            return "This is the Welcome action";
        }
    }
}
 
after this its working same as default .net mvc routing when call https://localhost:44365/HelloWorld its invoke index action method and if you call https://localhost:44365/HelloWorld/Welcome its invoke the Welcome action method. 
and if you want to apply default URL just change the routing format in startup.cs as per the  common MVC routing logic

/[Controller]/[ActionName]/[Parameters]
 
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
 // This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
        public void Configure(IApplicationBuilder app, IHostingEnvironment env)
        {
            if (env.IsDevelopment())
            {
                app.UseDeveloperExceptionPage();
            }
            else
            {
                app.UseExceptionHandler("/Home/Error");
                app.UseHsts();
            }

            app.UseHttpsRedirection();
            app.UseStaticFiles();
            app.UseCookiePolicy();

            app.UseMvc(routes =>
            {
                routes.MapRoute(
                    name: "default",
                    template: "{controller=HelloWorld}/{action=Welcome}/{id?}");
            });
        }
Add a View
 
 
 
Right click on the Views folder, and then Add > New Folder and name the folder HelloWorld.

Right click on the Views/HelloWorld folder, and then New Item -> Web ->Razor View 
name it as index.cshtml

and insdie the  index.cshtml
add the following code
 
@{
    ViewData["Title"] = "Index";
}

<h2>Index</h2>

<p>Hello from our Index View Template!</p>
 
 
  and in the HellowWorld HelloWorldController.cs change the index method as follow
  
        public IActionResult Index()
        {
            return View();
        }


after this the index.cshtml page load secussfully. 
from the next post ill connect the ASP.Net core app connected to SQL database with entity freamwork.





No comments:

Post a Comment