You can reduce the quality of photos to speed up the loading of your site
If you own a website or work on a customer website, then you need to know the importance of SEO. Without proper SEO strategies, you can not get high rankings in Google.
Success in a media channel such as organic search depends on the content. In particular, the production of useful content has the ability to rank. In its recent algorithm updates, Google has focused heavily on promoting great content and natural links, and penalizing poor content with unspecified links.
Websites are created using different frameworks and programming languages. The .NET framework is one of the many options you can choose from.
What is Middleware?
Middleware is a piece of code in the application pipeline that is used to control requests and responses.
Middleware can be embedded as part of the .NET Core framework, which is added via NuGet packages, or it can be custom middleware. These middleware components are configured as part of the program startup class in the configure method. Configure methods configure a pipeline processing request for an ASP.NET Core application that includes a string of delegate requests that are executed one after the other.
The image below shows how to process a request through middleware components.
In general, each middleware may control incoming requests and pass the execution to the next middleware for further processing.
But a middleware can decide not to call the next piece of middleware in the pipeline. It is called short-circuiting or terminate the request pipeline.
Let's create an ASP.NET Core Web application and see the default middleware configuration in the Startup class Configure method.
The ASP.NET Core framework provides some built-in middleware components that we can easily use in the Configure method.
The middleware components are executed in the order added to the pipeline, and care must be taken that the middleware is added in the correct order, otherwise the program may not work as expected. This arrangement is very important for security, efficiency and performance.
The following middleware components are recommended for common application scenarios, respectively:
The first configured middleware receives the request, modifies it (if necessary), and transfers control to the next firmware. Similarly, if the response is processed, if the echo returns to the bottom of the tube, the first middleware will run in the last step. This is why Exception-handling delegates are called early in the pipeline, so they can validate the result and show a possible exception in a browser-friendly way.
Understand how to run, use and Map Method
This middleware component is able to show the Run [Middleware] methods running at the end of the pipeline. Generally, it acts as a middleware terminal and a request pipeline is added at the end of the request processing process because it cannot call the next middleware.
app.Use() is used to configure multiple middleware. Unlike app.Run (), we can put the next parameter in it, which calls the next delegate request in the pipeline. We can also terminate the pipeline by not calling the next parameter.
Check the following example with app.Use () and app.Run () and also see output / response:
The first app.Run () terminates the pipeline. In this example, only the first delegate ("Hello from 1st app.Run ()") is executed and the request never reaches the second run method.
These extensions are used as a contract for a pipeline multi-line system. map Expands the request processing process based on the request path. If the request path starts with the given path, a new branch of middleware will run. In fact, you can divide the request processing process into several branches, each of which may run depending on the request path.
Check the following example with app.Map () and also see output / response:
The table below shows localhost requests and responses using the code above.
|()Hello from app.Run||https://localhost:44362/|
|()Hello from 1st app.Map||https://localhost:44362/m1|
|()Hello from 1st app.Map||https://localhost:44362/m1/xyz|
|()Hello from 2nd app.Map||https://localhost:44362/m2|
|()Hello from app.Run||https://localhost:44362/m500|
Middleware is generally encapsulated in a class and exposed with the method extension. Custom middleware can be created with a class by the InvokeAsync () method and the RequestDelegate type parameter in the constructor. In order to run the next middleware in a row, the RequestDelegate type is required.
Let's consider an example where we need to create custom middleware to register a URL request in a web application.
In the configure method:
Enable directory browsing with middleware
Directory browsing allows users of your web application to browse directory directories and files in a specific directory.
directory browsing is disabled by default for security reasons.
Let's consider an example where we want to accept a list of images in the browser under the images folder at wwwroot. UseDirectoryBrowser can control and submit those images to that request, and then short-circuit the rest of the pipeline.
So the middleware in ASP.NET Core controls how our application responds to HTTP requests.
In summary, each middleware component in ASP.NET Core: