我試圖根據當前活動的租戶返回一個視圖,但它不工作。由於某些原因,settings.Name是空的,即使它應該包含租戶網站的名稱。這裏是我的代碼:在Orchard CMS中使用ShellSettings獲取當前/活動租戶的名稱
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using Orchard.Themes;
using Orchard.Environment.Configuration;
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");
}
}
}
}
}
任何幫助將不勝感激。謝謝。
@ Bertrand Le Roy謝謝你能幫我澄清一些事情嗎?我如何着手配置白名單?我正在考慮將其作爲一個更好的選擇,但不知道如何實現這一目標。另外我將如何去使用「動作參數」,而不是訪問查詢字符串?再次感謝您的幫助。 – EmeraldArcher 2014-09-05 15:14:56
對不起,我有可能向SiteSettings內容部分添加新內容,以允許管理員用戶輸入任意數量的網址進入白名單,然後以訪問網站名稱的相同方式訪問白名單? – EmeraldArcher 2014-09-05 15:33:00
使其可配置:做一個網站設置(但你已經知道了,不是嗎?)。動作參數:只需要一個'string url'參數給你的動作。 – 2014-09-06 04:39:28