Determine Current Tenant Site

Topics: Customizing Orchard
Sep 4, 2014 at 4:08 PM
Hi all,

I need a way of determining which is the current tenant site being viewd so I can have my module's controller return a specific view based on which tenant is currently active. Is this possible? I haven't found a whole lot of documentation on this, but it appears there may be something to do with injecting the shell settings??? Any help would be greatly appreciated. Thank you.

As an end goal I would like my code to do something along the lines of:

if (wasValidURL) {
return View(currentTenantSite + "validURL");
} else {
return View(currentTenantSite + "invalidURL");
}

this way I can have a specific view per tenant site as each message on the exampletenantsitevalidURL.cshtml and exampletenantsiteinvalidURL.cshtml will be specific to the current tenant.
Sep 4, 2014 at 4:11 PM
As a side note I would like this to be taken care of in the controller if that is possible. My current controller has the following code in it:

public class SpeedBumpController : Controller
{
    [Themed]
    public ActionResult Index()
    {
        //Initialize Variables
        string requestedURL = "";
        string finalRequestedURL = "";
        bool wasValidURL = false;

        //Grab the query string parameters and put them into variables to be used later
            //Requested URL
            requestedURL = Request.QueryString["url"];

        //Remove "http://" or "https://" from the Requested URL (if it exists)
        if (requestedURL.IndexOf("http") > -1)
        {
            finalRequestedURL = requestedURL.Replace("http://", "");
        }
        else if (requestedURL.IndexOf("https") > -1)
        {
            finalRequestedURL = requestedURL.Replace("https://", "");
        }
        else
        {
            finalRequestedURL = requestedURL;
        }

        //Create a list of strings to contain all the "valid" URLs
        var whiteList = new List<string>();

        //Add URLs to the list
        whiteList.Add("www.google.com");
        whiteList.Add("www.gmail.com");

        //Loop through each URL in the list of Valid URLs checking against the finalRequestedURL
        foreach (string validURL in whiteList)
        {
            if (finalRequestedURL == validURL)
            {
                wasValidURL = true;
                break;
            }
            wasValidURL = false;
        }

        //ViewBag Items
        ViewBag.wasValidURL = wasValidURL;
        ViewBag.requestedURL = finalRequestedURL;

        //Return a different view depending on whether or not the url is valid
        if (wasValidURL)
        {
            return View("validURL");
        }
        else
        {
            return View("invalidURL");
        }
    }
}
It would be nice if I could have a variable in there that would get set to the name of the current tenant site and then I could create a specific view for each tenant site.
Developer
Sep 4, 2014 at 4:45 PM
why are you not creating a Uri and using the methods on that? doing a string replace is dangerous.

also.... use Linq....

ViewBag.wasValidURL = whiteList.Any(x => x == finalRequestedURL)

and use the ?? operator...

return ViewBag.wasValidURL ?? View("validURL") : View("invalidURL");

That code example you pasted could be around 4 lines of code.


Sep 4, 2014 at 4:46 PM
I included ShellSettings in my controller and believe this should work (see code), but for some reason it returns the default views instead of the settings.Name + ValidURL and settings.Name + InvalidURL views. Any ideas why this is happening?

Code:

public class SpeedBumpController : Controller
{
    [Themed]
    public ActionResult Index(ShellSettings settings)
    {
        //Initialize Variables
        string requestedURL = "";
        string finalRequestedURL = "";
        bool wasValidURL = false;

        //Grab the query string parameters and put them into variables to be used later
            //Requested URL
            requestedURL = Request.QueryString["url"];

        //Remove "http://" or "https://" from the Requested URL (if it exists)
        if (requestedURL.IndexOf("http") > -1)
        {
            finalRequestedURL = requestedURL.Replace("http://", "");
        }
        else if (requestedURL.IndexOf("https") > -1)
        {
            finalRequestedURL = requestedURL.Replace("https://", "");
        }
        else
        {
            finalRequestedURL = requestedURL;
        }

        //Create a list of strings to contain all the "valid" URLs
        var whiteList = new List<string>();

        //Add URLs to the list
        whiteList.Add("www.google.com");
        whiteList.Add("www.gmail.com");

        //Loop through each URL in the list of Valid URLs checking against the finalRequestedURL
        foreach (string validURL in whiteList)
        {
            if (finalRequestedURL == validURL)
            {
                wasValidURL = true;
                break;
            }
            wasValidURL = false;
        }

        //ViewBag Items
        ViewBag.wasValidURL = wasValidURL;
        ViewBag.requestedURL = finalRequestedURL;

        //Return a different view depending on whether or not the url is valid
        if (wasValidURL)
        {
            if (!string.IsNullOrEmpty(settings.Name))
            {
                return View(settings.Name + "ValidURL");
            }
            else
            {
                return View("ValidURL");
            }

        }
        else
        {
            if (!string.IsNullOrEmpty(settings.Name))
            {
                return View(settings.Name + "InvalidURL");
            }
            else
            {
                return View("InvalidURL");
            }
        }
    }
}
Developer
Sep 4, 2014 at 4:47 PM
another refactor from my previous example...

return whiteList.Any(x => x == finalRequestedURL) ?? View("validURL") : View("invalidURL");

No need to use viewBag, you are going to a view with the name, so you know if its true or not.


Developer
Sep 4, 2014 at 4:48 PM
or...

return whiteList.Contains(finalRequestedURL, StirngComparison.IgnoreCase) ?? View("validURL") : View("invalidURL");


Sep 4, 2014 at 4:51 PM
After further investigation it appears settings.Name is empty. Am I missing something?
Sep 4, 2014 at 5:04 PM
Edited Sep 4, 2014 at 5:05 PM
I apoligize for the length of the code I am new to C#, .net, etc. I'm used to programming websites in plain html, javascript, css, php, etc.

What is a Uri and how do I use it instead of the replace method I am currently using and how do I use Linq? Also any ideas why settings.Name is empty?

Thank you for your help and patience. As you can tell I'm rather new to all of this.