2014-09-04 37 views
0

我試圖根據當前活動的租戶返回一個視圖,但它不工作。由於某些原因,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"); 
       } 
      } 
     } 
    } 
} 

任何幫助將不勝感激。謝謝。

回答

1

這不是依賴注入的工作方式。您不通過操作參數注入,而是通過構造函數參數注入。這是第一個問題。

第二個問題是,你不應該注入ShellSettings,而是IWorkContextAccessor,然後做.GetContext().CurrentSite就可以得到站點設置。

這應該回答你的問題,但在你的代碼中也有其他問題。

例如,你不應該訪問查詢字符串。改爲使用操作參數並讓框架執行模型綁定。

之後,你正在測試URL中任何地方的「http」,這將產生誤報,並且還會捕獲「https」,所以其他事情永遠不會被達到。請改用StartsWith和「http://」。

然後,請不要使用Replace:如果您知道網址以「http://」開頭,則可以使用SubString(7)

不要在代碼中硬編碼您的白名單。相反,使這個可配置。

學會使用Contains而不是編寫不必要的循環。

不要在這裏使用ViewBag:這似乎不是一個很好的理由。

看來你想要做的是返回重定向結果,而不是視圖。

+0

@ Bertrand Le Roy謝謝你能幫我澄清一些事情嗎?我如何着手配置白名單?我正在考慮將其作爲一個更好的選擇,但不知道如何實現這一目標。另外我將如何去使用「動作參數」,而不是訪問查詢字符串?再次感謝您的幫助。 – EmeraldArcher 2014-09-05 15:14:56

+0

對不起,我有可能向SiteSettings內容部分添加新內容,以允許管理員用戶輸入任意數量的網址進入白名單,然後以訪問網站名稱的相同方式訪問白名單? – EmeraldArcher 2014-09-05 15:33:00

+0

使其可配置:做一個網站設置(但你已經知道了,不是嗎?)。動作參數:只需要一個'string url'參數給你的動作。 – 2014-09-06 04:39:28