0

我正在使用来自Dynamic CSS using Razor Engine的示例,我有以下内容:

这是在我的 _Layout.cshtml 中:

<link href="@Url.Action("Index", "Css")" rel="stylesheet" type="text/css" />

这是我的 CssController:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using RazorEngine;

namespace SuburbanCustPortal.Controllers
{
    public class CssController : Controller
    {
        //
        // GET: /Css/

        public string Index(string id)
        {
          Console.WriteLine("id: " + id);
          Response.ContentType = "text/css";
          return Razor.Parse(System.IO.File.ReadAllText(Server.MapPath("/Content/Site.css")));
        }

    }
}

我的问题出在哪里,我需要在他们第一次访问该站点时传入的 guid (id) 上加载一个特定的 CSS 文件。上面索引中的 id 显示为 null,但它正在加载。

例子:

mysite.com/account/login/xxxx-xxxx-xxxx-xxxx/

我需要根据该 guid 加载 css。

是否可以从 _Layout.cshtml 中获取该值,或者是否有另一种处理方式?

4

1 回答 1

1

您可以将该 Guid 放入会话中:

账户控制人:

public ActionResult LogIn(Guid identifier)
{
     Session["Identifier"] = identifier;
     return View();
}

在布局中:

<link href="@Url.Action("Index", "Css", new { id = Session["Identifier"] })" rel="stylesheet" type="text/css" />

CSS控制器:

public string Index(Guid id)
    {
      Console.WriteLine("id: " + id);
      Response.ContentType = "text/css";
      return Razor.Parse(System.IO.File.ReadAllText(Server.MapPath("/Content/Site.css")));
    }

您可以随时从任何控制器更改会话变量

于 2012-12-15T13:46:18.933 回答