【问题标题】:Using ShellSettings in Orchard CMS to get the Current/Active Tenant's Name在 Orchard CMS 中使用 ShellSettings 获取当前/活动租户的名称
【发布时间】:2014-09-04 15:58:50
【问题描述】:

我正在尝试根据当前活动的租户返回一个视图,但它不起作用。由于某些原因 settings.Name 为空,即使它应该包含租户站点的名称。这是我的代码:

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");
                }
            }
        }
    }
}

任何帮助将不胜感激。谢谢。

【问题讨论】:

    标签: module controller views orchardcms


    【解决方案1】:

    这不是依赖注入的工作方式。您不是通过动作参数注入,而是通过构造函数参数注入。这是第一个问题。

    第二个问题是你不应该注入ShellSettings,而是IWorkContextAccessor,然后在其上执行.GetContext().CurrentSite 以获取站点设置。

    这应该可以回答您的问题,但您的代码中还有其他问题。

    例如,您不应该访问查询字符串。改用动作参数,让框架进行模型绑定。

    在那之后,您将在 url 中的任何位置测试“http”,这会产生误报,并且还会捕获“https”,因此永远不会到达 else。请改用StartsWith 和“http://”。

    那么,不要使用Replace:如果你知道URL以“http://”开头,你可以使用SubString(7)

    不要在代码中硬编码您的白名单。相反,让它可配置。

    学习使用Contains,而不是编写不必要的循环。

    不要在这里使用ViewBag:似乎没有很好的理由。

    看起来你想要做的是返回重定向结果,而不是视图。

    【讨论】:

    • @ Bertrand Le Roy 感谢您的帮助,您能帮我澄清几件事吗?如何使白名单可配置?我一直认为这样做是一个更好的选择,但不知道如何实现这一目标。另外,我将如何使用“动作参数”而不是访问查询字符串?再次感谢您的所有帮助。
    • 对不起,我是否可以向 SiteSettings 内容部分添加新内容,允许管理员用户将任意数量的 url 输入将成为白名单的内容,然后以相同的方式访问该白名单我正在访问网站的名称?
    • 使其可配置:将其设置为站点设置(但您已经弄清楚了,不是吗?)。操作参数:只需为您的操作添加一个 string url 参数。
    【解决方案2】:

    我不知道这是在哪个版本中添加的,但是使用 Orchard 1.10,您可以直接(在构造函数中)注入 SiteSettings,而无需绕过 IWorkContextAccessor

    【讨论】:

      猜你喜欢
      • 2012-05-27
      • 1970-01-01
      • 2013-06-25
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-12-18
      • 2013-01-01
      • 1970-01-01
      相关资源
      最近更新 更多