Copied to clipboard

Flag this post as spam?

This post will be reported to the moderators as potential spam to be looked at


  • Saied 349 posts 674 karma points
    Jun 02, 2016 @ 18:45
    Saied
    0

    How can I make the following action method thread safe?

    The following two links access the same action method (they share the same document type and access the same controller and action):

    http://stage.bullydog.com/Products/accessories/podmount and http://stage.sctflash.com/Products/accessories/podmount

    I use Request.Url.Host to determine the brand of products I want to return from a database. When I access http://stage.bullydog.com/Products/accessories/podmount first, Request.Url.Host contains the value stage.bullydog.com, but if I then go to http://stage.sctflash.com/Products/accessories/podmount, Request.Url.Host may contain stage.sctflash.com or it may contain stage.bullydog.com.

    The action method that is called is:

    public ActionResult GetAccessoriesByType(RenderModel model, string id)
        {
            Common _common = new Common();
            string brand = Request.Url != null ? _common.GetProductBrand() : BrandType.SCT;
    
            var productSearchResultsModel = new ProductSearchResultsModel
            {
                Accessories = _accessoryRepository.GetAccessoriesByType(id, brand)
            };
    
            return View("~/Views/accessories.cshtml", productSearchResultsModel);
        }
    

    The code that gets the brand is:

    public class Common
    {
        public string GetProductBrand()
        {
            var host = HttpContext.Current.Request.Url.Host;
    
            if (host.Contains("sctflash"))
                return BrandType.SCT;
    
            if (host.Contains("bigrig") || host.Contains("bigrigs"))
                return BrandType.BigRig;
    
            if (host.Contains("bullydog"))
                return BrandType.BullyDog;
    
            return BrandType.SCT;
        }
    }
    

    How can I ensure the Request.Url.Host contains the proper host when the same action method is accessed from two different hosts?

    You can see this in action if you go to http://stage.bullydog.com/Products/accessories/podmount and then go to http://stage.sctflash.com/Products/accessories/podmount and refresh either one, the logo should change which means that the value returned by Request.Url.Host was incorrect.

    Also, can GetProductBrand be static and still be thread-safe in this case?

  • Saied 349 posts 674 karma points
    Jun 03, 2016 @ 05:27
    Saied
    0

    The reason why accessing another website was bringing back the one websites content and the Request.Url.Host had the other websites information was because I had declared OutPutCache[Duration=60] at the top of my controller. If I waited 60 seconds and then refreshed, the correct data for each website would return. Once I removed this from the controller, everything worked as expected.

  • Alex Skrypnyk 6182 posts 24283 karma points MVP 8x admin c-trib
    Jul 03, 2016 @ 11:05
    Alex Skrypnyk
    0

    Hi Saied,

    You can also setup your cache per domain.

    Try to look at http://stackoverflow.com/questions/2012072/asp-net-mvc-output-cache-for-multinenant-application-vary-by-hostname-and-cultu

    You need something like:

    [OutputCache(Duration = 3600, VaryByParam = "none", VaryByHeader = "host")]
    

    Thanks,

    Alex

  • This forum is in read-only mode while we transition to the new forum.

    You can continue this topic on the new forum by tapping the "Continue discussion" link below.

Please Sign in or register to post replies