Render CSS with Razor Engine + Enabling One Request for Multiple Files
Here is my situation...
public partial class CssController : Controller
{
public virtual ActionResult Merge(string[] files)
{
var builder = new StringBuilder();
foreach (var file in files)
{
var pathAllowed = Server.MapPath(Url.Content("~/Content/css"));
var normalizeFile = Server.MapPath(Url.Content(Path.Combine("~/Content/css", file)));
if (normalizeFile.StartsWith(pathAllowed) == false)
{
return HttpNotFound("Path not allowed");
}
if (System.IO.File.Exists(normalizeFile))
{
Response.AddFileDependency(normalizeFile);
builder.AppendLine(System.IO.File.ReadAllText(normalizeFile));
}
}
Response.Cache.VaryByParams["files"] = true;
Response.Cache.SetLastModifiedFromFileDependencies();
Response.Cache.SetETagFromFileDependencies();
Response.Cache.SetCacheability(HttpCacheability.Public);
var css = dotless.Core.Less.Parse(builder.ToString(), new DotlessConfiguration());
return Content(css, "text/css");
}
public string Index()
{
Response.ContentType = "text/css";
return Razor.Parse(System.IO.File.ReadAllText(Server.MapPath("~/Content/css开发者_运维知识库/Global.css")));
}
}
}
Now the public action method was used to get all css files on one request. Also reutrning that and parsing .less files. Well due to lack of editors that support .less, we moved to Razor to parse our css. The bottom string method displays what we are trying to do there. Well plain and simple, how can I integrate both those methods without getting an error.
In our Layout, we are using this
<link rel="stylesheet" type="text/css" href="@Url.ActionLinkWithArray("Merge", "Css", new { files = new[] { "StoreManager.less" } })" />
to pull all files on one request. Yet this is using the .less Configuration when I wnat to use the RazorEngine.
I hope this explains what I need to do. Can anyone help?
My favorite solution is Cassette by Andrew Davey.
In your _Layout.cshtml
you:
<head>
<title>@ViewBag.Title</title>
@Assets.Scripts.Render()
</head>
And in any view that requires javascript:
@{
ViewBag.Title = "My Page";
Assets.Scripts.Reference("scripts/utils/date.js");
Assets.Scripts.Reference("scripts/widgets/calendar.js");
}
This will minify (includes Less
) and condense all your scripts and css into a single files for each.
This may or may not help but sure does make it easy. It looks as if you're looking for a solution for your current problem but this is definitely an alternative.
精彩评论