有没有办法从MVC控制器拦截页面生命周期并在页面或DOM上的某处注入代码?我想在页眉和页脚上添加HTML,CSS和JavaScript,但不将它们添加到嵌入在某个程序集中的_Layout视图中。所以我想从Global.asax / Application_Start的控制器做到这一点。这可能吗?
答案 0 :(得分:1)
我做了类似的事情。
首先,您需要在布局文件中添加占位符(您要在其中注入代码)
其次,您需要在global.asax中添加全局ActionFilterAttribute。 在FilterAttribute中,仅覆盖OnActionExecuted过滤器并仅过滤ViewResults。
第三,您需要更改Response.Filter,使用一个新的过滤器来搜索html代码中的占位符,并将其替换为您自己的。
我写到了这一点,我添加了一种方式来声明Styles&代码中的任何位置的脚本,当结果完成时,资源将被注入头标记。
public class DynamicHeaderHandler : ActionFilterAttribute
{
private static readonly byte[] _headClosingTagBuffer = Encoding.UTF8.GetBytes("</head>");
private static readonly byte[] _placeholderBuffer = Encoding.UTF8.GetBytes(DynamicHeader.SectionPlaceholder);
public override void OnActionExecuted(ActionExecutedContext filterContext)
{
if (filterContext.Result is ViewResult)
{
filterContext.HttpContext.Response.Filter = new ResponseFilter(filterContext.HttpContext.Response.Filter);
}
base.OnActionExecuted(filterContext);
}
private class ResponseFilter : MemoryStream
{
private readonly Stream _outputStream;
public ResponseFilter(Stream aOutputStream)
{
_outputStream = aOutputStream;
}
public override void Close()
{
_outputStream.Flush();
_outputStream.Close();
}
public override void Write(byte[] buffer, int offset, int count)
{
var maxIndex = IndexOfSubArray(buffer, _headClosingTagBuffer);
var index = IndexOfSubArray(buffer, _placeholderBuffer, maxIndex);
if (index == -1)
{
_outputStream.Write(buffer, offset, count);
}
else
{
var html = DynamicHeader.GetHtml();
var injectBytes = Encoding.UTF8.GetBytes(html);
var newBuffer = buffer.Take(index).Concat(injectBytes).Concat(buffer.Skip(index + _placeholderBuffer.Length)).ToArray();
_outputStream.Write(newBuffer, 0, newBuffer.Length);
}
}
private static int IndexOfSubArray(byte[] aBuffer, byte[] aSubBuffer, int aMaxIndex = int.MaxValue)
{
if (aBuffer.Length < aSubBuffer.Length)
{
return -1;
}
for (var outerIndex = 0; outerIndex < aBuffer.Length && outerIndex < aMaxIndex; ++outerIndex)
{
var isEqual = true;
for (int subInnerIndex = 0, innerIndex = outerIndex; subInnerIndex < aSubBuffer.Length && innerIndex < aBuffer.Length; ++subInnerIndex, ++innerIndex)
{
if (aBuffer[innerIndex] != aSubBuffer[subInnerIndex])
{
isEqual = false;
break;
}
}
if (isEqual)
{
return outerIndex;
}
}
return -1;
}
}
}
我附上了ActionFilterAttribute,这可能与您有关。
如果您想查看整个代码,可以访问: Add Styles & Scripts Dynamically