Load time is one of the important characteristics of website or application performance as it unveils the success of a site. Hence, if a website/app is taking more than 3 seconds to load, customers/users might leave the site and never come back. Hence businesses prefer to have a perfect and robust web app. ASP.NET Core is the best technology for this. ASP.NET Core is an open-source, free, and cross-platform web development system that is created and handled by Microsoft. Though lots of developers are using it, it is necessary to know the best practices so as to improve the website/app performance. Hence here we came with some of the best practices. But before digging into it, let’s have an overview of ASP.NET Core.
It is an open-source, lightweight, cross platform, fast and re-write of Asp.Net framework that runs on various operating systems like Windows, Linux, and Mac. Speed is one of the important feature of Asp.Net core so that it is optimized for better performance.
Also know- Top 15 .NET Core Libraries That You Must Know
So as to improve app performance, it is better to ensure that you build apps that use few amount of resources to generate desired outcomes. Let’s have a look at best practices to improve .Net core app performance.
Optimizing data access logic of app is one of the most important best practices to improve the performance. Most of the apps are fully dependent on the database and they have to get data from the database, process it and display it.
It is suggested that-
Caching is a popular way to improve performance. You should cache to store data that is relatively stable. ASP.NET Core offers response caching middleware support that you can use to enforce response caching. You can use response caching to improve output caching and it can cache web server responses using cache-related headers to HTTP response objects. Also, caching large objects avoids costly allocations. Here are some of the caching techniques:
Memory cache can be used or a distributed cache like NCache or Redis Cache can be used.
Reduction in response size can improve the performance app because it transfers less data between server and client. You can exploit response compression in ASP.Net Core to reduce requirements of bandwidth and lower the response. It acts as sure-shot middleware components in ASP.Net Core.
public void ConfigureServices(IServiceCollection services_collection) { services_collection.AddResponseCompression(); services_collection.Configure<GzipCompressionProviderOptions> (opt => { opt.Level = CompressionLevel.Fastest; }); }
Except if they are required before, you should consistently strive to load our JS files at the end. As a result, your website will load faster and users won’t need to wait to see the information.
If there are only numbered CSS and JS files then it is simple to load on the server. For bigger static files, you can think about using CDN. Most of the CDNs have numerous locations and serve files from local server. Website performance can be improved by loading files from a local server.
Exceptions must be rare. Throw and catch exceptions are slow in comparison to other code flow patterns. Exceptions are not used to regulate the flow of program. Consider the logic of the program to identify and resolve exception-prone scenarios. Catch and throw exceptions for unusual or unexpected conditions. You can use tools like app insights to identify common exceptions in an app and how they perform.
You can provide detailed names and should use NOUNS rather than VERBS for routes/endpoints.
Don’t-
[Route("api/route- employee")] public class EmployeeController : Controller { [HttpGet(“get-all-employee”)] public IActionResult GetAllEmployee() { } [HttpGet("get- employee-by-Id/{id}"] public IActionResult GetEmployeeById(int id) { } }
Do:
[Route("api/employee")] public class EmployeeController : Controller { [HttpGet] public IActionResult GetAllEmployee() { } [HttpGet("{id}"] public IActionResult GetEmployeeById(int id) { } }
Swagger is a representation of RESTful API that allows interactive documentation, discoverability and client SDK support. You just need a minute to setup Swagger tool.
There are many auto-generated codes, hence keep some time to examine the logic flow, and because you know your app better, you can improve it better.
Read more