4

I'm using Sitecore 9.1.1, and a multisite setup.

I have a custom controller rendering that I view from a page in Site2, my controller had 2 methods: an Index() method that returns the view, and a Submit() method that does something else (called by ajax)

What happens is, inside my Index method, Sitecore.Context.Site.Name returns "Site2" correctly, however when I debug inside my Submit() method, Sitecore.Context.Site.Name returns "Site1" for some bizarre reason!

I have tried setting a dummy variable with a value and checking what happens inside each of Index() and Submit(), I tried this code:

    public string x = "x";

    public ActionResult Index()
    {
        x = "y";
        return View();
    }

    [HttpPost]
    public string Submit(exampleModel ex)
    {
       ...
    }

It turns out, as soon as my breakpoint inside Submit() is hit, "x" is reset back to its original value, I think Sitecore.Context.Site is being reset as well, I have no idea why this is happening though.

I need to read the Context.Site.Name correctly, does anyone know why this happens or what I did to cause the reset?

3
  • How do you configure the callback url?
    – Mark Cassidy
    Commented Aug 6, 2019 at 14:08
  • @MarkCassidy I used an ajax call whose url parameter is "mySite/MyController/Submit" and I send my data in it as formData Commented Aug 6, 2019 at 14:31
  • 1
    a dummy variable is useless, for each request new instance of controller is created.
    – x3mxray
    Commented Aug 6, 2019 at 15:12

1 Answer 1

6

Sitecore does not resolve the Sitecore.Context properly in case of the HttpPost through AJAX requests. I would suggest to add the siteName as a parameter to your Action as part of the ExampleModel and pass this data in the AJAX request, example code below, then you can fetch the needed site by the name.

public ActionResult Index()
{
    return View(new ExampleModel 
    {
        SiteName = Sitecore.Context.Site.Name
        /* add other fields this view model if needed */
    });
}

[HttpPost]
public string Submit(ExampleModel ex)
{
   var matchedSite = Sitecore.Configuration.Factory.GetSiteInfoList().First(site => site.Name == ex.SiteName);
}
1
  • 1
    Yeah I ended up doing a similar solution, I thought I was doing something wrong and that's why Sitecore.Context was incorrect, thanks for the answer :) Commented Aug 6, 2019 at 15:35

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.