asp.net mvc - Overriding OnActionExecuted and Getting Weird IIS Responses -


so,

i have mvc 2 site on iis 6 (wildcard application maps/aspnet_isapi.dll hack) want direct people different urls depending on preferred location, let's la.acme.com vs. nyc.acme.com.

i not want end @ www.acme.com or acme.com.

to accomplish doing following (i'll admit may not best way, why asking question):

in base controller, doing following:

protected override void initialize(system.web.routing.requestcontext requestcontext)     {          returnurl = requestcontext.httpcontext.request.url.pathandquery;          baseurl = requestcontext.httpcontext.request.url.host;         if (!requestcontext.httpcontext.request.url.isdefaultport) {             baseurl += ":" + requestcontext.httpcontext.request.url.port;         }         viewdata["baseurl"] = baseurl;          base.initialize(requestcontext);     }  protected override void onactionexecuted(actionexecutedcontext filtercontext)     {           var route = filtercontext.requestcontext.routedata;         var controller = route.getrequiredstring("controller");         var action = route.getrequiredstring("action");         string baseplushttp = "http://" + baseurl;         string baseplushttps = "https://" + baseurl;          string actionstoskip = "choosecity|displayphoto|chooselocation|press|contactus|aboutus|termsofservice|privacypolicy|logon|register|prospect|";          if (!actionstoskip.contains(action + "|")) {             if (baseurl.contains("www.acme.com") || baseplushttp.startswith("http://acme.com") || baseplushttps.startswith("https://acme.com")) {                 if (filtercontext.httpcontext.request.cookies["storelocationid"] != null && convert.toint32(filtercontext.httpcontext.request.cookies["storelocationid"].value) > 0) {                     filtercontext.httpcontext.response.redirect("/home/choosecity/" + convert.toint32(filtercontext.httpcontext.request.cookies["storelocationid"].value) + "?returnurl=" + returnurl);                 } else {                     aspnet_user user = repository.getaspnet_user(filtercontext.httpcontext.user.identity.name);                     if (user != null) {                         if (user.storelocationid != null) {                             int storelocationid = convert.toint32(user.storelocationid);                             filtercontext.httpcontext.response.redirect("/home/choosecity/" + storelocationid + "?returnurl=" + returnurl);                         } else {                             //get location ip                             string zip = getzipfromip();                             if (!string.isnullorempty(zip)) {                                 var zipcode = repository.getzipcode(zip);                                 if (zipcode != null) {                                     var storelocation = repository.getstorelocationbyzipcodeid(zipcode.zipcodeid);                                     if (storelocation != null) {                                         filtercontext.httpcontext.response.redirect("/home/choosecity/" + storelocation.storelocationid + "?returnurl=" + returnurl);                                     } else {                                         filtercontext.httpcontext.response.redirect("/home/chooselocation/");                                     }                                 } else {                                     filtercontext.httpcontext.response.redirect("/home/chooselocation/");                                 }                             } else {                                 filtercontext.httpcontext.response.redirect("/home/chooselocation/");                             }                         }                     } else {                         //get location ip                         string zip = getzipfromip();                         if (!string.isnullorempty(zip)) {                             var zipcode = repository.getzipcode(zip);                             if (zipcode != null) {                                 var storelocation = repository.getstorelocationbyzipcodeid(zipcode.zipcodeid);                                 if (storelocation != null) {                                     filtercontext.httpcontext.response.redirect("/home/choosecity/" + storelocation.storelocationid + "?returnurl=" + returnurl);                                 } else {                                     filtercontext.httpcontext.response.redirect("/home/chooselocation/");                                 }                             } else {                                 filtercontext.httpcontext.response.redirect("/home/chooselocation/");                             }                         } else {                             filtercontext.httpcontext.response.redirect("/home/chooselocation/");                         }                      }                 }             } else { //make sure storelocation chookie value correct based on url...we should not have nyc.acme.com url 'la' cookie.                 if (baseurl.contains("nyc.")) {                     if (filtercontext.httpcontext.request.cookies["storelocationid"] == null) {                         filtercontext.httpcontext.response.redirect("/home/choosecity/3?returnurl=" + returnurl);                     } else {                         if (filtercontext.httpcontext.request.cookies["storelocationid"].value != "3") {                             filtercontext.httpcontext.response.redirect("/home/choosecity/3?returnurl=" + returnurl);                         }                     }                 }                  if (baseurl.contains("la.")) {                     if (filtercontext.httpcontext.request.cookies["storelocationid"] == null) {                         filtercontext.httpcontext.response.redirect("/home/choosecity/5?returnurl=" + returnurl);                     } else {                         if (filtercontext.httpcontext.request.cookies["storelocationid"].value != "5") {                             filtercontext.httpcontext.response.redirect("/home/choosecity/5?returnurl=" + returnurl);                         }                     }                 }             }         }              base.onactionexecuted(filtercontext);     } 

here's summary of lengthy code in onactionexecuted above:

  1. if user on "action" allow www.acme.com or acme.com (like privacypolicy), don't bother going further;

  2. if url contains "www" or not contain city prefix, let's move on need fix situation;

  3. if have cookie (storelocationid) specifies preferred city, redirect /home/choosecity set other cookies , redirect correct url: nyc.acme.com or la.acme.com.

  4. if signed-in, , if user account lists preferred city, use (redirect /home/choosecity -- same 3 above)

  5. they have no cookie , no stored in user account, use ip zip , see if zip in or around city cover, if send /home/choosecity in 3 & 4 above

  6. finally, if cannot determine city they'd prefer, have them choose manually @ /home/chooselocation/

this works. but, starting notice following @ random times...

sometimes (and no seeming pattern) user click link , end @ random page (not destination of clicked link). seem location end @ location user (cities apart) may have been requesting (i know you're thinking quantum entanglement, i've ruled out). it's if iis getting confused whom asked what.

my question (after that) can logic above (in onactionexecuted) causing iis trip up? should noted "random locations" problem arises users nyc or la in url...users have proper storelocation cookie. means user should never redirected "/home/choosecity" or "/home/chooselocation" have proper configuration of url , cookies.

the final answer using static classes/properties in places not appropriate. did not understand such static classes shared across entire web application (meaning users). assumed contained 1 user @ time.

the weird behavior result of race conditions on shared properties.


Comments

Popular posts from this blog

asp.net - repeatedly call AddImageUrl(url) to assemble pdf document -

java - Android recognize cell phone with keyboard or not? -

iphone - How would you achieve a LED Scrolling effect? -