ASP.NET Core is an open source, cross platform framework for building modern, cloud based web apps on Windows, macOS, or Linux.
Follow below steps to create ASP.NET Core web app.
– Create a web app project
– Trust the development certificate
– Run the app.
There are few prerequisites before starting,
– .NET SDK (as of today there are multiple version available like 6.0/7.0/8.0 Preview)
– An IDE (Visual Studio/ Visual Studio Code etc…)
Now will start the new web app, open a command shell and enter the following command> dotnet new webapp -o myfirstcoreapp
This will creates a new web app. The -o myfirstcoreapp parameter in above command creates a directory named myfirstcoreapp with source files for the app.
Once the web app is created then run another command to trust the development certificate, basically it will trust the HTTPS development certificate.
> dotnet dev-certs https --trust
It will prompt a security warning window, select Yes if you are agree to trust the development certificate.
Now its time to tun the web app, first move to web app folder and then run the web app.> cd myfirstcoreapp
> dotnet watch run/ dotnet run
If build is success then command shell indicates that the app has started, browse to https://localhost:5001
The Project Files
The solution will have different sections which contains folders/files that will create the UI.
Pages folder
It contains Razor pages and supporting files. Each razor page is a pair of files:
- .cshtml file that has HTML markup with C# code using Razor syntax
- .cshtml.cs file that has C# code that handles the page events.
There are few supporting files which names start with underscore (_), like _Layout.cshtml file configures UI elements common to all the pages. Generally it will have navigation menus at the top of the page and copyright information at the bottom of the page.
wwwroot folder
It will contain static assets, like HTML files, JavaScript, and CSS files etc..,
appsettings.json (we can use different for development and production like appsettings.Development.json)
Contains configuration data like log, connection strings and some other config values.
Program.cs
it will be created with some default configuration code like
It creates a WebApplicationBuilder with preconfigured defaults, and Razor pages support to the Dependency Injection (DI) container, and build the app.
var builder = WebApplication.CreateBuilder(args); // Add services to the container. builder.Services.AddRazorPages(); var app = builder.Build();
The developer exception page is enabled by default and it provides helpful information on exceptions. For production it should be risky to show that information as it may expose sensitive information. So in production environment it will redirects to a endpoint /Error and also enable HTTP Strict Transport Security Protocol (HSTS).
// Configure the HTTP request pipeline. if (!app.Environment.IsDevelopment()) { app.UseExceptionHandler("/Error"); // The default HSTS value is 30 days. You may want to change this for production scenarios, see https://aka.ms/aspnetcore-hsts. app.UseHsts(); }
And also have some middleware configurations in Program.cs file.
app.UseHttpsRedirection(); app.UseStaticFiles(); app.UseRouting(); app.UseAuthorization(); app.MapRazorPages(); app.Run();
Do some changes in Razor pages, enjoy the coding.
/Siva