What is a View ?
}
Why is it Bad ?
Best Way :
Tip 04: Uninstall URL Rewrite if you don’t use it
Important Note: MVC 2 always performs this check.So turning
URL Rewrite off (uninstall) will not make a difference
When to Use it ?
- Incoming browser requests are mapped to controller actions
- A controller action might return a View
- View handles the display of the data
- Most often the Views are created from the ViewModel data
How to Improve View Performance ?
Tip 01 : Run in Release Mode
- You should always make sure that your application is Compiled in Release Mode
- Your Web.config file Must Looks Like below on Production
<compilationdebug="false"targetFramework="4.0">
<assemblies>
</assemblies>
</compilation>
Why This Needs ?
Why This Needs ?
# MVC Will Not do any View Look-up Caching if you are running your application
in Debug Mode
in Debug Mode
Tip 02: Use only the View Engines that you need
- MVC framework supports having multiple view engines
- Can Configured simultaneously in your application
- And will query each in turn when it is trying to find a View
- In MVC 3 we register two view engines by default (WebForms and Razor)
- There is no reason to pay the performance price for something you are not using
- So make sure you specify only the view engines you need in your Global.asax file
Global.asax file Looks Like below :
protectedvoidApplication_Start()
{
ViewEngines.Engines.Clear();
ViewEngines.Engines.Add(newRazorViewEngine());//add razor view engine
Tip 03: Avoid passing null ViewModels to Views
When it Happens ?
- You pass in a null ViewModel to a View that uses strongly-typed html helpers
Such as in the View :
@modelPawLoyalty.ViewModels.Product
@{
@Html.TextBoxFor(m => m.ProductName);
}
- This frequently happens in Insert (Add) scenarios
- Strongly-typed html helpers like above,
- Will try to emit the value of the ViewModel using the provided expression
- However when something along the expression chain is null a NullReferenceException will be thrown,
- When the expression gets evaluated,
- MVC’s expression evaluator catches the exception
- But on a page with multiple such html helpers the cost of the exception adds up
- You can avoid that cost by always passing an instance of the ViewModel to the View
Bad Way :
[HttpGet]
publicActionResult Add() //get empty data for user input operation
{
returnView(); //here the model instance defaults to null
}
[HttpGet]
publicActionResult Add()//get empty data for user input operation
{
returnView(newProduct());//here the ViewModel Product has been sent
}
When it Happens :
- Your IIS server has the URL Rewrite module installed
- When you are running ASP.NET MVC 3,
- None of the applications on the server are using it
What is URL Rewrite ?
# Enables Web administrators to create powerful rules to implement URLs
# That are easier for users to remember and easier for search engines to find
Why is it Bad ?
How to Uninstall URL Rewrite ?
# Win 7 --> Controller Panel --> Programs and Features -->
# Enables Web administrators to create powerful rules to implement URLs
# That are easier for users to remember and easier for search engines to find
Why is it Bad ?
- When performing URL generation (e.g : Html.ActionLink)
- MVC 3 checks to see if the currently requested URL has been rewritten by the URL Rewrite module
- The act of checking need significant computing power to solve (because it involves checking server variables)
- So if you are not using URL Rewrite you should Turn it Off (Uninstall)
How to Uninstall URL Rewrite ?
# Win 7 --> Controller Panel --> Programs and Features -->
Important Note: MVC 2 always performs this check.So turning
URL Rewrite off (uninstall) will not make a difference
Tip 05: Enabling Output Caching
What is Output Caching ?
What is Output Caching ?
- Enables you to cache the content returned by a controller action
- The same content does not need to be generated each and every time the same controller action is invoked
- Caching enables you to avoid performing redundant work on the Server
- Can apply for either individual controller action or an entire controller class
How to Do That ?
- Using [OutputCache]attribute
Lets take a simple Example
publicclassHomeController : Controller
{
[OutputCache(Duration = 10, VaryByParam = "none")] //cached for 10 seconds
publicActionResult Index()
{
returnView();
}
}
- Read operations Only
When Not to Use it ?
- Writes operations.Such as Update,Delete and Inserts
- Pages which are User Specific.e.g. shopping cart page
Summery of Performance Gain Chart :
Pages/Sec - How many pages can load per second
Page Time (ms) - How long will it take to load a page
Base - Page that has not done any performance gain operations
Conclusion
- You can see that highest performance gain can get by using OutputCache operation
- But OutputCache having it's own Limitations
- I will explain more about MVC caching in My future blog post
- If your page is load under 1 second then you may not worry too much about performance gain
I hope this helps to You.Comments and feedback greatly appreciated.
Happy Coding.
Happy Coding.
MVC Related Articles