MVC2 Partial View throws/catches exceptions while resolving the path to the view
While debugging my Visual Studio 2010 MVC2 solution, I noticed something that I didn't necessarily expect and that struck me as inefficient. When I was viewing the Home page, the IntelliTrace showed that many exceptions were getting thrown and caught while resolving a partial view.
I have code that calls the partial view like this:
<% Html.RenderPartial("FiltersAvailablePartialView", Model.AvailableFilters); %>
The structure of the views in the project follows a typical MVC application. There is a Views folder which contains two folders, Home and Shared. In the Home folder I have the .aspx page that is used as my main view page, and in the Shared folder I have a .ascx page that is used as a partial view, as shown below (with other views and partial views not listed for simplicity):
Views
-->Home
-->Home.aspx
-->Shared
-->FiltersAvailablePartialView.ascx
What I see in the IntelliTrace when I load the page is that these exceptions are thrown and caught:
Thrown: "The file '/MyApp/Views/Home/FiltersAvailablePartialView.aspx' does not exist."
Caught: "The file '/MyApp/Views/Home/FiltersAvailablePartialView.aspx' does not exist."
Thrown: "The file '/MyApp/Views/Home/FiltersAvailablePartialView.ascx' does not exist."
Caught: "The file '/MyApp/Views/Home/FiltersAvailablePartialView.ascx' does not exist."
Thrown: "The file '/MyApp/Views/Shared/FiltersAvailablePartialView.aspx' does not exist."
Caught: "The file '/MyApp/Views/Shared/FiltersAvailablePartialView.aspx' does not exist."
So it appears that the .Net framework is using expensive try/catch methods to resolve a view--looking first for a .aspx view in the Home folder, then for a .ascx view in the Home folder, th开发者_如何学Goen for a .aspx view in the Shared folder, then for a .ascx view in the Shared folder. So while it eventually finds the partial view and the page renders without any errors, this seems like an expensive way (catching exceptions) to find the view.
Is there something built-in with the MVC2 framework I could/should do to make it resolve this partial view reference on the first attempt and avoid these costly try/catch calls that are happening?
If you compile in Release mode, this happens only once. If you compile in Debug mode, it happens for every request.
Profiling in Debug mode is often of very limited relevance to real-world performance.
精彩评论