这不是您现在正在寻找的答案,但是如果您确实重新考虑使用具有不同配置的同一站点,请查看我最近为动态更改 CSS 等所做的工作:
我决定网站的每个“版本”都将在查询字符串中使用唯一的引用。基于此,我会找到正确的内容,将路径加载到模型中并将其发送到视图。
这是控制器:
public ActionResult Index()
{
List<string> listOfAcceptableRef = new List<string>() { "uniqueCode1", "uniqueCode2" };
string uniqueRef=null;
if (Request.QueryString["ref"]!=null)
policyRef = Request.QueryString["ref"].ToLower();
if (string.IsNullOrWhiteSpace(uniqueRef) || (!string.IsNullOrWhiteSpace(uniqueRef) && !listOfAcceptableRef.Contains(uniqueRef)))
{
throw new Exception("This reference key is unknown.");
//return RedirectToAction("KeyError");
}
return View(GetPageContext(uniqueRef));
}
从查询字符串中获取参考代码,然后从上下文工厂生成一个包含相关 CSS 路径的模型。
这是我的模型:
public class PageContext
{
public string Ref { get; set; }
public string TabId { get; set; }
public string TabName { get; set; }
public string SiteTitle { get; set; }
public string CssPath { get; set; }
public PageContext()
{
Products = new List<ProductInfo>();
}
}
还有我的上下文工厂:
public class ContextFactory<T>
{
private ContextFactory()
{
}
static readonly Dictionary<string, Type> _dict = new Dictionary<string, Type>()
{
{ "uniqueRef1", Type.GetType("The.Full.Page.Namespace.UniqueSite1Context")},
{ "uniqueRef2", Type.GetType("The.Full.Page.Namespace.UniqueSite2Context")}
};
public static bool Create(string reference, out T context)
{
context = default(T);
Type type = null;
if (_dict.TryGetValue(reference, out type))
{
context = (T)Activator.CreateInstance(type);
return true;
}
return false;
}
}
以及带有 CSS 路径等的实际上下文实例:
public class UniqueSite1Context : PageContext
{
public UniqueSite1Context()
{
this.Ref = "uniqueSite1";
this.CssPath = "Content/UniqueSite1Context.css";
this.DisclaimerPath = "Content/UniqueSite1Context.pdf";
this.SiteTitle = "UniqueSite1";
}
}
毕竟,只需使用模型的路径渲染 CSS:
@section Styles {
@{
string path = Url.Content("~") + Model.CssPath;
<link href="@path" rel="stylesheet" type="text/css" />
}
}
从架构上讲,您可以扩展它(或者更确切地说是它的概念)以根据用户访问的“站点”使用不同的逻辑和数据上下文。