Recently, I submitted a tip to Red Gate for their “50 Ways to Avoid, Find and Fix ASP.NET Performance Issues” e-book.Â If you haven’t had a chance to download it, you should because it’s free.
My tip was called “Remove Unused View Engines”, and showed you how to disable the WebForms view engine from getting in the way of your Razor view engine.Â A lot of folks don’t realize that you can do this, and that this small performance tweak can save you a little bit of time in the long run.
Let’s discuss how ASP.NET MVC handles View Engines.Â I’m specifying MVC, because if you’re using WebForms you don’t have access to the Razor view engine.
Let’s imagine that a request comes into your web server for http://yourdomain.com/.Â I’m assuming you’re familiar with basic ASP.NET MVC routing, and understand that his request will be forwarded to your Home controller and Index action.Â The default action for Index is to render a View called Index.
This is where the process becomes interesting.Â ASP.NET MVC really has no idea where you’ve decided to put the Index view.Â It determines the view location but using a series of conventions that are built in.
Have you ever seen a screenshot like this?
ASP.NET MVC is being really nice to you by divulging the locations it’s searching for your view.Â What’s happening behind the scenes?Â Here’s how ASP.NET MVC interacts with the View Engines.
ASP.NET: What View Engines do I have loaded?Â I see WebFormsViewEngine and RazorViewEngine.Â Hey WebFormsViewEngine, are you able to render a view called Index that came from the Home controller?
WebFormsViewEngine:Â Let me check.Â Is there a file called Views/Home/Index.aspx?Â No. Is there a file called Views/Home/Index.ascx? No.Â Ok ok.Â Maybe there is an Index.aspx or Index.ascx in the Views/Shared directory?Â No.Â Sorry, ASP.NET, I don’t have a view to render.
ASP.NET: Hey RazorViewEngine!Â Can you render a view called Index that came from the Home controller?
RazorViewEngine: I’ll see what I can do.Â Is there a file called Views/Home/Index.cshtml?Â No.Â How about Index.vbhtml?Â Nothing.Â Ok, I’ll check in the Views/Shared directory too.Â Nothing I can do, ASP.NET.
Â ASP.NET: Thanks for checking.Â ERROR.
Excuse the dramatization, but it’s accurate of how the conversation works in ASP.NET.Â If you’re developing solely in Razor with MVC, you’re spending a decent amount of time per request to locate views that will never exist.
My tip was to remove the WebForms view engine in your Global.asax file.
Tada!Â You’re done with WebForms.Â Beware, don’t do this if you’re actually using the WebForms view engine.Â You’ll get an error screen.
It might be interesting to visualize this with Glimpse.Â Here is a website that uses both view engines by default:
Compare it to a screen shot of the same site using only the Razor view engine.
If we have two view engines, we see that ASP.NET is doing two lookups per view engine in the worst case scenario.Â First, ASP.NET checks it’s cache for the view engine (more on that in a moment).Â If it’s not found, it attempts to render it with the view engine.
In the scenario, we have a view that contains a partial view.Â This means there are a total of 8 lookups before ASP.NET finally finds what it’s looking for.
In the second screen shot, we only see 4 lookups.Â If you have a site with multiple partial views, you’ll see faster lookups by reducing the number of locations ASP.NET needs to go.
How much does this actually save you?Â Here is the dirty truth, if you’re running in release mode, you’ll gain almost nothing from this.Â MVC is really good about caching its lookup results when running in release (note the lookups that Glimpse told us about).Â Take the conversation between ASP.NET and the view engines, and add in an additional comment where ASP.NET makes a note of how to render the view.
In Debug mode, this will pay dividends over and over again.Â In fact, depending on how you stage your benchmarking tests, removing an unused view engine can give you a huge boost in performance.Â We’re talking about milliseconds though.Â For a second request, a couple milliseconds doesn’t seem like much.Â It’s when you start to add up thousands of requests, the performance gains will really start to shine through.
Closing up, this is a good tip and should be a first step in any new ASP.NET MVC application where you know WebForms will not be used.Â Your development experience will be fantastic.Â When you’re ready to deploy, make sure you flip the appropriate Debug-to-Release switches, and enjoy even better performance.
Kevin Griffin is the owner of Griffin Consulting, Inc and is the Chief Technology Officer for Winsitter, Inc.Â He’s an ASP.NET MVP and an ASPInsider.Â He blogs on occasion over at http://www.kevgriffin.com and tweets all the time at @1kevgriff.Â If you’re looking for a great developer for your next project, give Kevin a call.