您可以结合使用 2 个自定义 HTML 帮助程序来替换现有的服务器端控件:
public static class HtmlExtensions
{
private const string GlobalsQueueKey = "_globals_queue_";
private const string ReadyWrapperQueueKey = "_ready_wrapper_queue_";
[Obsolete("Don't use inline scripts, that's what javascript files are meant for. And yeah, there are pretty nifty javascript compressors out there. Not to mention that in ASP.NET MVC 4 there's the bundling and minification functionality built-in. So use this helper just for fun not in a real application that you intend to put in production.")]
public static IHtmlString RegisterInlineScript(this HtmlHelper htmlHelper, bool compress, bool useReadyWrapper, Func<object, HelperResult> action)
{
var queueKey = useReadyWrapper ? ReadyWrapperQueueKey : GlobalsQueueKey;
var queue = htmlHelper.ViewContext.HttpContext.Items[queueKey] as Queue<Func<object, HelperResult>>;
if (queue == null)
{
queue = new Queue<Func<object, HelperResult>>();
htmlHelper.ViewContext.HttpContext.Items[queueKey] = queue;
}
queue.Enqueue(action);
return MvcHtmlString.Empty;
}
[Obsolete("Don't use inline scripts, that's what javascript files are meant for. And yeah, there are pretty nifty javascript compressors out there. Not to mention that in ASP.NET MVC 4 there's the bundling and minification functionality built-in. So use this helper just for fun not in a real application that you intend to put in production.")]
public static IHtmlString InlineScripts(this HtmlHelper htmlHelper)
{
var globalsQueue = htmlHelper.ViewContext.HttpContext.Items[GlobalsQueueKey] as Queue<Func<object, HelperResult>> ?? new Queue<Func<object, HelperResult>>();
var readyWrapperQueue = htmlHelper.ViewContext.HttpContext.Items[ReadyWrapperQueueKey] as Queue<Func<object, HelperResult>> ?? new Queue<Func<object, HelperResult>>();
if (!globalsQueue.Any() && !readyWrapperQueue.Any())
{
// Nothing to compress, nothing to output
return MvcHtmlString.Empty;
}
var writer = htmlHelper.ViewContext.Writer;
writer.Write("<script type=\"text/javascript\">");
using (var globalsWriter = new StringWriter())
{
foreach (var item in globalsQueue)
{
item(null).WriteTo(globalsWriter);
}
var globals = globalsWriter.GetStringBuilder().ToString();
writer.Write(Compress(globals));
}
using (var readyWrapperWriter = new StringWriter())
{
foreach (var item in readyWrapperQueue)
{
item(null).WriteTo(readyWrapperWriter);
}
var readyWrapper = readyWrapperWriter.GetStringBuilder().ToString();
writer.Write(
string.Format("$(function() {{{0}}});", Compress(readyWrapper))
);
}
writer.Write("</script>");
return MvcHtmlString.Empty;
}
private static string Compress(string script)
{
// TODO: wheel reinvention code from your existing
// server side control to be put here to compress
return script;
}
}
然后您可以在其中注册多个内联脚本的视图:
@Html.RegisterInlineScript(true, false,
@<text>
var myGlobalVar = "something";
</text>
)
@Html.RegisterInlineScript(true, true,
@<text>
var _registrationViewModel = new RegisterViewModel();
</text>
)
@Html.RegisterInlineScript(true, true,
@<text>
var _addNewUserViewModel = new AddNewUserViewModel();
</text>
)
在你的_Layout
输出中的某个地方:
@Html.InlineScripts()
好的,现在为您的实际应用程序检查 ASP.NET MVC 4 中的捆绑和缩小支持:http ://www.beletsky.net/2012/04/new-in-aspnet-mvc4-bundling-and.html