ASP.NET MVC3 app creating bad route value?

I would not call static values here. I completely disagree with Igor above. Its not standard and is hard to track down where these values are coming from to someone that doesn't know the app.

Call it either from your controller, or even better yet - a service layer (ie business logic) your controller calls to that gets this value.

Up vote 1 down vote favorite share g+ share fb share tw.

OK, this may just me being ignorant, but I have the following route in my MVC3 application: routes. MapRoute("Directory","{aid}/{controller}/{action}/{year}/{quarter}", new { aid = "sf", controller = "Lobbyist", action = "Index", year = CurrentYear(), quarter = CurrentQuarter() }); In my Global.asax. Cs, I have these two methods: 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; } This code works great almost all the time. At one point in time, in our production environment (running IIS7), the route value for CurrentQuarter() became a value of zero, when it should have been 1, 2, 3, or 4. It works just fine in production except for that one point in time.

An IISRESET 'fixed' the problem. What I know: At the time CurrentQuarter() was failing, CurrentYear() was still returning correctly The CurrentQuarter() method was not throwing an exception which would have prevented setting the 'quarter' variable The business logic which drives the CurrentQuarter() method works for every date between DateTime. MinValue and DateTime.

MaxValue My question really gets down to is: Is it BAD to call static methods to generate route values? Is there a potential for the application to 'forget' the result of the static method, and cause it to return a garbage value? Could an application pool recycling cause this?

I'm sort of grasping at straws here! Thanks for any advice. Asp.

Net-mvc-3 link|improve this question asked Nov 3 '11 at 19:46David Montgomery37719 100% accept rate.

The answers are "No" and "No". In the default Global.asax. Cs for a new MVC project RegisterRoutes (is your routes.

MapRoute called there? ) is a static method which is called on Application_Start. CurrentQuarter is called only once during this registration.

The supplied value for quarter url element is used only if it is not present in the request url. – Igor Nov 3 '11 at 20:02.

I would not call static values here. I completely disagree with Igor above. Its not standard and is hard to track down where these values are coming from to someone that doesn't know the app.

Call it either from your controller, or even better yet - a service layer (ie business logic) your controller calls to that gets this value. A routes purpose is not to call a business method. On the second question, if there is an app pool recycle, the value will simply be reset.

However if multiple threads are calling into this method and potentially changing the value in the same method I would implement some locking in there to prevent updates from overlapping.

I see your point about it being more difficult to debug for a developer who doesn't know the code backwards. I do have a question about your last comment 'implement some locking'. Do you mean that a static method like my CurrentYear() method above isn't thread-safe?

I would find that truly shocking. I could see if I was setting some variable's value declared outside the method, but otherwise... – David Montgomery Nov 8 '11 at 22:59.

You should make the route values year and quarter optional, and provide default values for them in the Action method. I think this makes everything cleaner, and easier to maintain. Public class LobbyistController { public ActionResult Index(int?

Year, int? Quarter) { if (!currentYear. HasValue) { currentYear = GetCurrentYear(); } if (!currentQuarter.

HasValue) { currentQuarter = GetCurrentQuarter(); } // the rest } }.

I cant really gove you an answer,but what I can give you is a way to a solution, that is you have to find the anglde that you relate to or peaks your interest. A good paper is one that people get drawn into because it reaches them ln some way.As for me WW11 to me, I think of the holocaust and the effect it had on the survivors, their families and those who stood by and did nothing until it was too late.

Related Questions