Module 使用Orchard CMS中的ShellSettings获取当前/活动租户';姓名
我试图返回一个基于当前活动租户的视图,但它不起作用。由于某些原因,设置。名称为空,即使它应该包含租户站点的名称。这是我的密码:Module 使用Orchard CMS中的ShellSettings获取当前/活动租户';姓名,module,controller,views,orchardcms,Module,Controller,Views,Orchardcms,我试图返回一个基于当前活动租户的视图,但它不起作用。由于某些原因,设置。名称为空,即使它应该包含租户站点的名称。这是我的密码: using System; using System.Collections.Generic; using System.Linq; using System.Web; using System.Web.Mvc; using Orchard.Themes; using Orchard.Environment.Configuration; namespace Speed
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");
}
}
}
}
}
使用系统;
使用System.Collections.Generic;
使用System.Linq;
使用System.Web;
使用System.Web.Mvc;
使用果园主题;
使用Orchard.Environment.Configuration;
命名空间Speedbump.Controllers
{
公共类控制器:控制器
{
[主题]
公共操作结果索引(外壳设置)
{
//初始化变量
字符串requestedURL=“”;
字符串finalRequestedURL=“”;
bool wasValidURL=false;
//获取查询字符串参数,并将它们放入变量中以供以后使用
//请求的URL
requestedURL=Request.QueryString[“url”];
//从请求的URL(如果存在)中删除“http://”或“https://”
if(requestedURL.IndexOf(“http”)>-1)
{
finalRequestedURL=requestedURL.Replace(“http:/”,“”);
}
else if(requestedURL.IndexOf(“https”)>-1)
{
finalRequestedURL=requestedURL.Replace(“https:/”,“”);
}
其他的
{
finalRequestedURL=请求的Durl;
}
//创建包含所有“有效”URL的字符串列表
var whiteList=新列表();
//将URL添加到列表中
白名单添加(“www.google.com”);
白名单(www.gmail.com);
//循环检查有效URL列表中的每个URL,并对照finalRequestedURL进行检查
foreach(白名单中的字符串validURL)
{
if(finalRequestedURL==validURL)
{
wasValidURL=真;
打破
}
wasValidURL=假;
}
//查看包项目
ViewBag.wasValidURL=wasValidURL;
ViewBag.requestedURL=finalRequestedURL;
ViewBag.tenantName=设置。名称;
//根据url是否有效,返回不同的视图
如果(wasValidURL)
{
如果(!string.IsNullOrEmpty(settings.Name))
{
返回视图(settings.Name+“validull”);
}
其他的
{
返回视图(“Validull”);
}
}
其他的
{
如果(!string.IsNullOrEmpty(settings.Name))
{
返回视图(settings.Name+“InvalidURL”);
}
其他的
{
返回视图(“InvalidURL”);
}
}
}
}
}
任何帮助都将不胜感激。谢谢。这不是依赖注入的工作方式。您不通过操作参数进行注入,而是通过构造函数参数进行注入。这是第一个问题 第二个问题是,您不应该插入
ShellSettings
,而应该插入IWorkContextAccessor
,然后在其上执行.GetContext().CurrentSite
,以获取站点设置
这应该可以回答您的问题,但代码中还有其他问题
例如,您不应该访问querystring。改用动作参数,让框架进行模型绑定
在这之后,您将在url中的任何位置测试“http”,这将产生误报,并且还会捕获“https”,因此永远无法访问其他地址。相反,使用StartsWith
和“http://”
然后,不要使用Replace
:如果您知道URL以“http://”开头,您可以使用子字符串(7)
不要用代码硬编码你的白名单。相反,要使其可配置
学习使用包含而不是编写不必要的循环
不要在这里使用ViewBag
:似乎没有很好的理由这样做
看起来你想做的是返回重定向结果,而不是视图。@Bertrand Le Roy谢谢你的帮助你能帮我澄清几件事吗?我该如何配置白名单?我想这样做是一个更好的选择,但不知道如何实现。还有,我如何使用“动作参数”而不是访问querystring?再次感谢您的帮助。对不起,我是否可以向SiteSettings内容部分添加新内容,允许管理员用户在将成为白名单的内容中输入任意数量的URL,然后以与访问网站名称相同的方式访问该白名单?使其可配置:使其成为网站设置(但你已经明白了,不是吗?)。动作参数:只需为你的动作设置一个字符串url
参数。