ASP.NET 8 Best Practices: Coding, Performance Tips, and Tricks

In this chapter, we will explore various best practices and performance tips to enhance your ASP.NET 6 applications. By following these recommendations, you can improve the efficiency, maintainability, and overall performance of your code. We will cover topics such as memory caching, error handling, early function return, optimizing form access, hosting considerations, JIT optimization, and managing application secrets.

  1. Efficient Memory Caching:
    1.1 Utilize the AddOrCreate API in the .NET MemoryCache to minimize code
          The AddOrCreate API gives you the benefit of checking for cache keys before adding them. It works the same
          magic as "Create OR Alter" in SQL when creating a Stored Procedure.

  2. Error Handling:
    Make sure every Function has Error Handling. For Example, check if the input is valid or not to handle the appropriate Error. In programming, you just don't trust what the User is going to input into the function.
    Implementing Error Handling also allows returning from the function as soon as possible to improve performance.

    For example, the code below checks if id is equal to zero, if true it returns to the caller otherwise the process continues.

    public string getNameByID(int id){
    //Error Handling
      if (id == 0) return;
    //Otherwise Continue

  3. Optimizing Form Access:
    Like shown in 2, make sure you return from the function as soon as possible. You can do this by first trying to retrieve or check whether the record exists if not return without executing further Code Instructions.

    - This speeds up the Application as the User will be notified with further instructions on how to proceed.
    - Returning as soon as possible from the function also saves Computer Memory and CPU Threads. If the input or
       any one of the criteria not met that allows the function to execute, return to the caller when this
       constraint is known. The best way to visualize this is when using a Recursion Algorithm.

    public string MyFunction(int n)
        if (n < = 1) // base case
            return 1;//return as soon as possible.
            return n*fact(n-1);    

  4. Demian over at Microsoft explains how using a Form API in Asp.Net Core would potentially affect the application performance. This is because the HttpContext does not know upfront that there is a Form attached to the incoming request until you try to access the IHttpFeature to get the values of the form. The HttpRequest Items are lazily loaded into the Context hence they are expensive to access.

    David Fowler illustrated how to Get a Form value from Asp.Net Core HttpContext

      He said the best way to access the form content is by using ReadFormAsync():
        var form = await httpContext.Request.ReadFormAsync()
      He also mentioned that:
       var form = httpContext.Request.Form;//This is bad

    [Personally have not tested and verified the above statement, however, they are the Developers for Asp.Net Core]

  5. The Dotnet Team is planning to phase out the Out-Of-Process hosting in IIS, so if you host your app in IIS start looking into hosting with In-Process.

  6. Be careful using Sessions in Asp.Net Core, it might bring a bottleneck into the application.

  7. JIT in Asp.Net Core becomes self-optimized after the Application runs for a while. The Just In Time Compilation underlying Software becomes Operating System aware and then optimizes the Hot Path of the code execution based on the running System or Hardware (Linux or Windows).

    The question becomes though, does this optimization end when you deploy a new build? If the JIT saves the Optimization metadata to disk and then loads them back into Memory on the next application deployment that would be great. If you know to answer this question, please comment below.

    I guess this is the question of AOT and PGO, if you could do some research on that you will be able to answer the question above.

  8. Personal Lessons Learned Developing Asp.Net Core Applications
    Do not hardcode the environment variables in the code, those credentials change from time to time and you do not want to refactor the code every time they change. 

Instead, put all your secret credentials like User Names and Password in the secret file or Configuration file for none secret credentials.

In conclusion, implementing best practices and applying performance optimization techniques is essential for developing high-performing ASP.NET 6 applications. By adhering to the recommendations discussed in this chapter, such as efficient memory caching, thorough error handling, early function return, optimized form access, informed hosting choices, understanding JIT optimization, and proper management of application secrets, you can significantly improve the performance, maintainability, and security of your ASP.NET 6 applications.

Remember to regularly review and refine your codebase to ensure adherence to these best practices and stay up to date with the latest advancements in ASP.NET 6. By continuously striving for optimization, you can deliver fast, responsive, and reliable web applications that meet the demands of modern software development.


1. Read the Article Here, it is very helpful to know the tips and tricks.
2. Watch this video of David Fowler and Damian Edwards explaining the Asp.Net Core Architecture. This is important because you would want to know how things work under the hood.

© 2024 - ErnesTech - Privacy
E-Commerce Return Policy