【发布时间】:2011-11-03 19:46:23
【问题描述】:
好的,这可能只是我无知,但我的 MVC3 应用程序中有以下路线:
routes.MapRoute("Directory","{aid}/{controller}/{action}/{year}/{quarter}",
new { aid = "sf", controller = "Lobbyist", action = "Index",
year = CurrentYear(), quarter = CurrentQuarter() });
在我的 Global.asax.cs 中,我有这两种方法:
public static int CurrentQuarter()
{
int quarter = 0;
//...use some internal business logic to determine the value of
//'quarter' based on the current date...
return quarter;
}
public static int CurrentYear()
{
return DateTime.Now.Year;
}
这段代码几乎一直都很好用。在某个时间点,在我们的生产环境(运行 IIS7)中,CurrentQuarter() 的路由值变为零值,而它本应为 1、2、3 或 4。它在生产环境中工作得很好,除了那个时间点。 IISRESET“修复”了问题。
我知道的:
- CurrentQuarter() 失败时,CurrentYear() 仍在 正确返回
- CurrentQuarter() 方法未抛出 会阻止设置“季度”变量的异常
- 驱动 CurrentQuarter() 方法的业务逻辑有效 对于 DateTime.MinValue 和 DateTime.MaxValue 之间的每个日期
我的问题真正归结为:
- 调用静态方法生成路由值是不是很糟糕?
- 应用程序是否有可能“忘记”静态方法的结果并导致它返回垃圾值?应用程序池回收会导致这种情况吗?
我有点想抓住稻草了!
感谢您的建议。
【问题讨论】:
-
答案是“否”和“否”。在新 MVC 项目的默认 Global.asax.cs 中,RegisterRoutes(您的 routes.MapRoute 是否在那里调用?)是在 Application_Start 上调用的静态方法。 CurrentQuarter 在此注册期间仅调用一次。仅当请求 url 中不存在季度 url 元素时,才使用提供的值。
标签: asp.net-mvc-3