asp.net core mvc implements pseudo-static function
This article mainly introduces the relevant information of asp.net core mvc to implement pseudo-static function in detail. It has certain reference value. Interested friends can refer to it
In large website systems In order to improve system access performance, some infrequent content is often published as static pages, such as the product details page and news details page of the mall. Once this information is published, the frequency of changes will not be very high. If you still use If processed through dynamic output, it will definitely cause a huge waste of resources on the server. But we cannot independently create static pages for these contents, so we can use pseudo-static processing in the system. As for what pseudo-static is, you can search it on Baidu. Let's introduce here how to implement pseudo-static in asp.net core mvc.
In the mvc framework, view represents the view, and the result of its execution is the content ultimately output to the client browser, including html, css, js, etc. If we want to achieve staticization, we need to save the result of view execution as a static file and save it to a specified location, such as disk, distributed cache, etc., so that we can directly read the saved content the next time we access it. No need to execute business logic again. So what should asp.net core mvc do to achieve such a function? The answer is to use filters. In the MVC framework, multiple filter types are provided. Here we are going to use action filters. Action filters provide two time points: before the action is executed and after the action is executed. We can first determine whether the static page has been generated before the action is executed. If it has been generated, we can directly read the file content and output it, and the subsequent logic will be skipped. If there is no production, continue to go down, capture the results at this stage after the action is executed, and then save the static content generated by the results.
Then let’s come to the specific implementation code. First, we define a filter type, which we call StaticFileHandlerFilterAttribute. This class is derived from the ActionFilterAttribute provided in the framework. StaticFileHandlerFilterAttribute overrides the two methods provided by the base class: OnActionExecuted( After the action is executed), OnActionExecuting (before the action is executed), the specific code is as follows:
1 2 3 4 5 6 |
|
In OnActionExecuting, you need to determine whether the static content has been generated. If it has Generate direct output content, the logic implementation is as follows:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 |
|
In OnActionExecuted, we need the action result to determine whether the action result type is a ViewResult. If it is through code Execute this result, obtain the result output, and generate a static page according to the same rules as above. The specific implementation is as follows
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 |
|
For the Key mentioned above, we directly add the corresponding Attribute
1 2 3 4 |
|
So we can use this filter, the method used: add the [StaticFileHandlerFilter] attribute to the controller or controller method, if If you want to configure different Keys, you can use [StaticFileHandlerFilter(Key="set value")]
Staticization has been implemented, and we still need to consider updates. If an article is updated in the background, we You have to update the static pages. There are many solutions: one is to delete the corresponding static pages synchronously when the content is updated in the background. We introduce another one here, scheduled update, which means that the static page has a certain validity period and is automatically updated after this validity period. To implement this logic, we need to get the creation time of the static page in the OnActionExecuting method, and then compare it with the current time to determine whether it has expired. If it has not expired, the content will be output directly. If it has expired, continue to execute the following logic. The specific code is as follows:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 |
|
At this point, pseudo-static is achieved. The current processing method can only improve access performance to a certain extent, but it may not be enough for large portal systems. According to the method introduced above, other functional extensions can be carried out. For example, after generating a static page, it can be published to a CDN, or it can be published to a separate content server, etc. Regardless of the method, the implementation idea is the same.
The above is the entire content of this article. I hope it will be helpful to everyone's learning. I also hope that everyone will support the PHP Chinese website.
For more articles related to asp.net core mvc implementing pseudo-static functions, please pay attention to the PHP Chinese website!

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics





The usage methods of symbols in C language cover arithmetic, assignment, conditions, logic, bit operators, etc. Arithmetic operators are used for basic mathematical operations, assignment operators are used for assignment and addition, subtraction, multiplication and division assignment, condition operators are used for different operations according to conditions, logical operators are used for logical operations, bit operators are used for bit-level operations, and special constants are used to represent null pointers, end-of-file markers, and non-numeric values.

In C, the char type is used in strings: 1. Store a single character; 2. Use an array to represent a string and end with a null terminator; 3. Operate through a string operation function; 4. Read or output a string from the keyboard.

In C language, special characters are processed through escape sequences, such as: \n represents line breaks. \t means tab character. Use escape sequences or character constants to represent special characters, such as char c = '\n'. Note that the backslash needs to be escaped twice. Different platforms and compilers may have different escape sequences, please consult the documentation.

The difference between multithreading and asynchronous is that multithreading executes multiple threads at the same time, while asynchronously performs operations without blocking the current thread. Multithreading is used for compute-intensive tasks, while asynchronously is used for user interaction. The advantage of multi-threading is to improve computing performance, while the advantage of asynchronous is to not block UI threads. Choosing multithreading or asynchronous depends on the nature of the task: Computation-intensive tasks use multithreading, tasks that interact with external resources and need to keep UI responsiveness use asynchronous.

In C language, the main difference between char and wchar_t is character encoding: char uses ASCII or extends ASCII, wchar_t uses Unicode; char takes up 1-2 bytes, wchar_t takes up 2-4 bytes; char is suitable for English text, wchar_t is suitable for multilingual text; char is widely supported, wchar_t depends on whether the compiler and operating system support Unicode; char is limited in character range, wchar_t has a larger character range, and special functions are used for arithmetic operations.

In C language, char type conversion can be directly converted to another type by: casting: using casting characters. Automatic type conversion: When one type of data can accommodate another type of value, the compiler automatically converts it.

There is no built-in sum function in C language, so it needs to be written by yourself. Sum can be achieved by traversing the array and accumulating elements: Loop version: Sum is calculated using for loop and array length. Pointer version: Use pointers to point to array elements, and efficient summing is achieved through self-increment pointers. Dynamically allocate array version: Dynamically allocate arrays and manage memory yourself, ensuring that allocated memory is freed to prevent memory leaks.

The char array stores character sequences in C language and is declared as char array_name[size]. The access element is passed through the subscript operator, and the element ends with the null terminator '\0', which represents the end point of the string. The C language provides a variety of string manipulation functions, such as strlen(), strcpy(), strcat() and strcmp().
