web analytics

ASP.NET Core Introduction

Options
@2020-11-14 10:26:19

ASP.NET Razor

Razor is a markup syntax that lets you embed server-based code (Visual Basic and C#) into web pages. When a web page is called, the Razor view engine executes the server-based code inside the page before it returns the page to the browser. 

The Razor syntax is a template markup syntax, based on the C# programming language, that enables the programmer to use an HTML construction workflow. Instead of using the ASP.NET Web Forms (.aspx) markup syntax with <%= %> symbols to indicate code blocks, Razor syntax starts code blocks with an @ character and does not require explicit closing of the code-block. The Razor parser is smart enough to infer this from your code. This enables a compact and expressive syntax which is clean, fast and fun to type.

Razor Syntax for C#

  • C# code blocks are enclosed in @{ ... }
  • Inline expressions (variables or functions) start with @
  • To escape an @ symbol in Razor markup, use a second @ symbol
  • Code statements end with semicolon
  • Variables are declared with the var keyword, or the datatype (int, string, etc.)
  • Strings are enclosed with quotation marks
  • C# code is case sensitive
  • C# files have the extension .cshtml

Razor Helpers

ASP.NET helpers are components that can be accessed by single lines of Razor code. You can build your own helpers using Razor syntax, or use built-in ASP.NET helpers. Razor’s @helper syntax provides a convenient way to encapsulate rendering functionality into helper methods that you can re-use within individual view templates, or across all view templates within a project. 

Below is a short description of some useful Razor helpers:

  • Web Grid
  • Web Graphics
  • Google Analytics
  • Facebook Integration
  • Twitter Integration
  • Sending Email
  • Validation
@2020-11-14 15:18:29

ASP.NET Core Application Model

An ASP.NET Core application is a console application that contains a web server that serves as the entry point for a request. Microsoft provides, by default, a cross-platform web server called Kestrel. By adding an ASP.NET Core web server to your .NET Core app, your application can run as a web application. ASP.NET Core is composed of many small libraries that you can choose from to provide your application with different features. Some of the libraries are common and will appear in virtually every application you create, such as the ones for reading configuration files or performing logging. Other libraries build on top of these base capabilities to provide application-specific functionality, such as third-party logging-in via Facebook or Google.

ASP.NET Core application can act as the server-side application for a variety of different clients: it can serve HTML pages for traditional web applications, it can act as a REST API for client-side SPA applications, or it can act as an ad-hoc RPC service for client applications.

 

In a traditional web application built on ASP.NET, IIS is tightly coupled with the application and calls into specific points of your application.

When you build a web application with ASP.NET Core, browsers will still be using the same HTTP protocol to communicate with your application. The request process starts when a user’s browser sends an HTTP request to the server. A reverse-proxy server captures the request, before passing it to your application. In Windows, the reverse-proxy server will typically be IIS, and on Linux or macOS it might be NGINX or Apache. The request is forwarded from the reverse proxy to your ASP.NET Core web application. ASP.NET Core web application itself encompasses everything that takes place on the server to handle a request, including verifying the request is valid, handling login details, and generating HTML.

@2020-11-14 16:44:00

Middleware And Request Pipeline in ASP.NET Core

ASP.NET Core comes with advanced request processing through the pipeline configuration of the .NET Core middleware elements. 

  • The Request Pipeline is the mechanism by which requests are processed beginning with a Request and ending with a Response.
  • The pipeline specifies how the application should respond to the HTTP request. The Request arriving from the browser goes through the pipeline and back
  • The individual components that make up the pipeline are called Middleware.

Core Pipeline

In ASP.NET Core, middlewares are C# classes that can handle an HTTP request or response. Middleware can either:

  • Handle an incoming HTTP request by generating an HTTP response.
  • Process an incoming HTTP request, modify it, and pass it on to another piece of middleware.
  • Process an outgoing HTTP response, modify it, and pass it on to either another piece of middleware or the ASP.NET Core web server.

For example, a piece of logging middleware might note down when a request arrived and then pass it on to another middleware. Meanwhile, an image resizing middleware component might spot an incoming request for an image with a specified size, generate the requested image, and send it back to the user without passing it on.

The most important piece of middleware in most ASP.NET Core applications is the MvcMiddleware. This normally generates your HTML pages and API responses. Like the image resizing middleware, it typically receives a request, generates a response, and then sends it back to the user.

@2020-11-22 13:22:53

The following sequence of tutorials for an introduction to developing ASP.NET Core apps:

1. Follow a tutorial for the app type you want to develop or maintain.

App type Scenario Tutorial
Web app New server-side web UI development Get started with Razor Pages
Web app Maintaining an MVC app Get started with MVC
Web app Client-side web UI development Get started with Blazor
Web API RESTful HTTP services Create a web API
Remote Procedure Call app Contract-first services using Protocol Buffers Get started with a gRPC service
Real-time app Bidirectional communication between servers and connected clients Get started with SignalR

2. Follow a tutorial that shows how to do basic data access.

Scenario Tutorial
New development Razor Pages with Entity Framework Core
Maintaining an MVC app MVC with Entity Framework Core

Comments

You must Sign In to comment on this topic.


© 2024 Digcode.com