MVC 6 更改视图块的呈现位置
MVC 6 更改视图块的呈现位置
我的目标是创建一个类似于razor @section Scripts {...}语法的模拟,可以在视图和视图组件中同样有效。如果将JavaScript转换为Windows字符串,我可以通过帮助方法来实现这一点。然而,这将破坏智能感知功能,使您陷入字符转义困境,并且不允许您在渲染之前进行重复和排序脚本。
我希望以一种允许Visual Studio编辑器将JavaScript作为JavaScript进行编辑的方式来实现此目标。似乎我应该能够做到类似这样的事情:
@using (Html.BeginNamedScript($"StatDisplay{Model.UniqueId}"))
{
$.ajax({
url: "@Model.ResultUrl",
method:"POST"
})
.done(function (value) {
var statText = "@Model.DisplayFormat".replace(/\{\s * 0\s *\}/, value);
$("#@labelId").text(statText);
});
}
HtmlHelperExtension:
public static NamedScript BeginNamedScript(this IHtmlHelper htmlHelper, string name, params string[] dependancies)
{
return new NamedScript(htmlHelper.ViewContext, name, htmlHelper, dependancies);
}
NamedScript类:
using System;
using System.Diagnostics;
using System.IO;
using Microsoft.AspNet.Mvc.Rendering;
using Microsoft.AspNet.Mvc.ViewFeatures;
namespace WebUIB8.Helpers
{
public class NamedScript : IDisposable
{
private bool _disposed;
private readonly FormContext _originalFormContext;
private readonly ViewContext _viewContext;
private readonly TextWriter _writer;
private readonly string _name;
private readonly HtmlHelper _helper;
private readonly string[] _dependsOn;
public NamedScript(ViewContext viewContext, string name, params string[] dependsOn):this(viewContext, name, null, dependsOn)
{
}
internal NamedScript(ViewContext viewContext, string name, IHtmlHelper helper, params string[] dependsOn)
{
if (viewContext == null)
{
throw new ArgumentNullException(nameof(viewContext));
}
_name = name;
_dependsOn = dependsOn;
_helper = helper as HtmlHelper;
_viewContext = viewContext;
_writer = viewContext.Writer;
Debug.WriteLine("Beginning:\r\n" + _viewContext);
_originalFormContext = viewContext.FormContext;
viewContext.FormContext = new FormContext();
Begin();
}
public void Dispose()
{
Dispose(true);
GC.SuppressFinalize(this);
}
public void Begin()
{
//No beginning action needed
}
private void End()
{
Debug.WriteLine("Ending:\r\n" + _writer);
_helper?.AddJavaScript(_name, _writer.ToString(), _dependsOn);
}
protected virtual void Dispose(bool disposing)
{
if (!_disposed)
{
_disposed = true;
End();
if (_viewContext != null)
_viewContext.FormContext = _originalFormContext;
}
}
public void EndForm()
{
Dispose(true);
}
}
}
我尝试过使用以下方法将脚本渲染为字符串,但它在.RenderAsync调用内抛出异常,并造成页面中止并出现503.2错误:
private async Task
{
using (var sw = new StringWriter())
{
var newViewContext = new ViewContext(viewContext, viewContext.View, viewContext.ViewData, sw);
var razorView = newViewContext.View as RazorView;
razorView.RenderAsync(newViewContext).Wait();
sw.Flush();
return sw.ToString();
}
}
问题:
1. 我是否错过了一个更简单的解决方案?是否有一种更简单的方法来渲染Razor标记的结果并将其传递给HTML帮助方法?
2. 如何将@using块中的ViewContext渲染为文本?
3. 如何防止该ViewContext与其余视图一起渲染?(以便稍后在页面上呈现)