How to Access Site Name and Base URL Using ShellSettings on Orchard

Topics: Core, Customizing Orchard
Sep 4, 2014 at 11:36 PM
I am trying to access the current tenant's name by using ShellSettings in my controller but settings.Name always returns empty. Any ideas why? Thank you.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using Orchard.Themes;
using System.Timers;
using Orchard.Environment.Configuration;
using Orchard.MultiTenancy.ViewModels;

namespace Speedbump.Controllers
{
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;
        ViewBag.tenantName = settings.Name;

        //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");
            }
        }
    }
}
}
Sep 5, 2014 at 8:05 AM
How does the Shellsettings get injected in the Index method?

Doesn't it need to be injected in the ctor? :
private readonly ShellSettings _settings;

public SpeedBumpController (ShellSettings settings) {
    _settings = settings;
}

public ActionResult Index() {
    // Do something with the settings
}
Developer
Sep 5, 2014 at 3:20 PM
You cannot inject in to a ActionMethod, you inject in to the ctor.
Developer
Sep 5, 2014 at 3:30 PM
Okay... dude... take a look at this...
namespace Speedbump.Controllers {
    public class SpeedBumpController : Controller {
        private readonly ShellSettings _settings;

        public SpeedBumpController(ShellSettings settings) {
            _settings = settings;
        }

        public ActionResult Index(string url) {
            var whiteList = new List<string> {
                new Uri("www.google.com").Authority, 
                new Uri("www.gmail.com").Authority
            };

            return whiteList.Contains(new Uri(url).Authority) ? View(_settings.Name + "ValidURL") : View(_settings.Name + "InvalidURL");
        }
    }
}
Sep 5, 2014 at 4:12 PM
Thank you for the help, that makes more sense after seeing it written out. As I said before, I am brand new to all of this.