Tăng hiệu suất ứng dụng bằng ViewEngines trong ASP.NET MVC

Công cụ Razor view engine hỗ trợ cả C# views và VB views trong một ứng dụng duy nhất. Tuy nhiên, nếu chúng ta chỉ sẽ sử dụng một loại cú pháp cho nhất quán thì chúng ta có thể tối ưu RazorCSharpViewEngine để hạn chế tìm các views.

Mặc định của lớp RazorCSharpViewEngine.cs như sau:
Code

using System.Web.Mvc;
public class RazorCSharpViewEngine : RazorViewEngine
{
public RazorCSharpViewEngine()
{
AreaViewLocationFormats = new[]
{
"~/Areas/{2}/Views/{1}/{0}.cshtml",
"~/Areas/{2}/Views/Shared/{0}.cshtml"
};
AreaMasterLocationFormats = new[]
{
"~/Areas/{2}/Views/{1}/{0}.cshtml",
"~/Areas/{2}/Views/Shared/{0}.cshtml"
};
AreaPartialViewLocationFormats = new[]
{
"~/Areas/{2}/Views/{1}/{0}.cshtml",
"~/Areas/{2}/Views/Shared/{0}.cshtml"
};
ViewLocationFormats = new[]
{
"~/Views/{1}/{0}.cshtml",
"~/Views/Shared/{0}.cshtml"
};
MasterLocationFormats = new[]
{
"~/Views/{1}/{0}.cshtml",
"~/Views/Shared/{0}.cshtml"
};
PartialViewLocationFormats = new[]
{
"~/Views/{1}/{0}.cshtml",
"~/Views/Shared/{0}.cshtml"
};
}
}

Ví dụ dưới đây trong project của mình thì chỉ dùng Razor View nên trong file Global.ascx mình sẽ thêm đoạn code vào như sau:
Code

public void Application_Start()
{
// Clears all previously registered view engines.
ViewEngines.Engines.Clear();
// Registers our Razor C# specific view engine.
ViewEngines.Engines.Add(new RazorCSharpViewEngine());
}

Hi vọng với thủ thuật này, sẽ giúp ứng dụng của bạn đạt hiệu suất tốt hơn.