简体   繁体   中英

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 probe 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.

在发布模式下运行时,视图位置将被缓存。

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM