Does ASP.NET MVC 2.0 use exceptions for flow control?
Following code throws (handled) exceptions, it appears that MVC uses exceptions to test for control locations.
<% Html.RenderPartial("LogOnUserControl"); %>
Application works as expected, I can see that exception is thrown couple times trying different locations (Views, Shared). Same thing happens with other controls. Apparently MVC uses exceptions to pro开发者_运维百科be different possible locations for the file.
IIRC using exceptions for flow control is evil, and is not cool. So, am I doing something wrong, or MVC is cool no more?
Note: having IDE stop on all thrown exception makes debugging easier, and I normally leave it on. That's how I got to that exception from RenderPartial
.
It is not true that MVC 2.0 uses exceptions for control flow.
However, System.Web.dll v2.0 (the core component of ASP.NET up to .NET 3.5) has some inefficient APIs for instantiating objects from virtual paths. MVC 2.0 mitigates this problem by having a cache of view lookups. By default this cache is disabled during development so that the changes you make are immediately visible, which is why you are seeing these exceptions. On a real production server these exceptions would not occur after the lookups are cached.
As a side note, MVC 3 will be using new APIs added in .NET 4 so this should not be a problem anymore.
When running in Release mode view locations are cached.
精彩评论