简体   繁体   English

ASP.NET MVC 2.0是否使用流控制异常?

[英]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. 在代码抛出(处理)异常之后,似乎MVC使用异常来测试控制位置。

<% 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. 显然,MVC使用异常来探测文件的不同可能位置。

IIRC using exceptions for flow control is evil, and is not cool. IIRC使用流量控制的例外是邪恶的,并不酷。 So, am I doing something wrong, or MVC is cool no more? 那么,我做错了什么,或者MVC不再酷了?

Note: having IDE stop on all thrown exception makes debugging easier, and I normally leave it on. 注意:让IDE停止所有引发的异常会使调试更容易,我通常会将其保留。 That's how I got to that exception from RenderPartial . 这就是我从RenderPartial获得异常的方式。

It is not true that MVC 2.0 uses exceptions for control flow. MVC 2.0使用控制流异常并不是事实。

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. 但是,System.Web.dll v2.0(ASP.NET直到.NET 3.5的核心组件)具有一些低效的API,用于从虚拟路径实例化对象。 MVC 2.0 mitigates this problem by having a cache of view lookups. MVC 2.0通过具有视图查找缓存来缓解此问题。 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. 作为旁注,MVC 3将使用.NET 4中添加的新API,因此这不应该成为问题。

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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