Simple pseudo static function implemented by aspnetcore

Time:2020-6-17

Intro

In my activity room appointment project, there is an announcement module, which is similar to news release. I feel like news is basically a web page that will not be changed after being edited once when it is released. It is most suitable for static, After the static file is requested by the user, the server-side query database or even server-side rendering is no longer needed, which can improve the processing capacity of the server and optimize the user experience to a certain extent, and the static URL is relatively friendly to SEO.

Because my project is under development and it is not convenient to migrate, it uses pseudo static. It looks like *. HTML accessed. In fact, it is not HTML, but needs to be processed by the server.

GetStarted

Configure the routing information, pay attention to the order. The pseudo static route should be prior to the default route


app.UseMvc(routes =>
{
  routes.MapRoute("Notice", "/Notice/{path}.html", new
  {
    controller = "Home",
    action = "NoticeDetails"
  });

  routes.MapRoute(name: "areaRoute",
    template: "{area:exists}/{controller=Home}/{action=Index}");

  routes.MapRoute(
    name: "default",
    template: "{controller=Home}/{action=Index}");
});

Controller code:

/// <summary>
///Announcement details
/// </summary>
///< param name = "path" > access path < / param >
/// <returns></returns>
public async Task<ActionResult> NoticeDetails(string path)
{
  if (string.IsNullOrWhiteSpace(path))
  {
    return RedirectToAction("Notice");
  }
  try
  {
    var noticeBll = HttpContext.RequestServices.GetService<IBLLNotice>();
    var notice = await noticeBll.FetchAsync(n => n.NoticeCustomPath == path.Trim());
    if (notice != null)
    {
      notice.NoticeVisitCount += 1;
      await noticeBll.UpdateAsync(notice, x => x.NoticeVisitCount);

      return View(notice);
    }
    else
    {
      return RedirectToAction("Notice");
    }
  }
  catch (Exception ex)
  {
    Logger.Error(ex);
    throw;
  }
}

Actual effect:

https://reservation.weihanli.xyz/Notice/test-notice.html

notice details

Q&A

Q: Why do we do (pseudo) statics?

A: The *. HTML file under the site is conducive to the SEO optimization of the site, to Baidu and Google crawlers crawling your site, and the good SEO performance is conducive to improving the search ranking [the water of SEO is also very deep, and you can find your own information and research if you are interested in it].

Q: What is pseudo static? What is complete static? What’s the difference between them?

A: Pseudo static is for full static, which actually saves the content into a *. HTML file, while pseudo static is for the specified program to process and return the content by matching the URL to a certain pattern through server-side technology.

In short, there will be a static file in the (complete) static mode, but there will be no pseudo static mode, which requires more server-side processing.

Q: Which is better, pseudo static or full static?

A: The better way to ask this question is: “when is it appropriate to use pseudo statics? When is it appropriate to use full statics? ” Since a thing exists, it must have its reason of existence. Existence is reasonable. If there is no meaning of existence, it will die.

In general, full static will generate a *. HTML file based on a template. Full static does not need too much server-side processing. When the client requests this file, because it is a static file server, it will directly return the file content to the client without additional server processing.
Full statics reduce server stress.

There is no static *. HTML file on the pseudo static server, but rewrite is used in the server to rewrite the dynamic URL, so that the dynamic URL can be expressed as a static URL, so as to meet the static demand of the web page URL, but the web page is still called dynamically, which requires a lot of server-side processing, such as URL pattern matching, query data from the database.

Summary: Although the web page with completely static URL has the advantages of fast opening speed, if the content of the website is huge, it will inevitably make the volume of the website much larger, there will be a lot of static files, and it will be very troublesome to migrate the website. On the other hand, if the content of the website is large and the template is modified, it will be a relatively large workload when it is static again. In actual use, you still need to choose according to your actual needs.

Reference

• https://github.com/WeihanLi/ActivityReservation/blob/dev/ActivityReservation

summary

The above is a simple pseudo static implementation of aspnetcore introduced by Xiaobian to you. I hope it can help you. If you have any questions, please leave a message to me, and Xiaobian will reply you in time. Thank you very much for your support of the developepaer website!
If you think this article is helpful to you, welcome to reprint, please indicate the source, thank you!