gpt4 book ai didi

razor - Asp.net Core 如何渲染 View

转载 作者:行者123 更新时间:2023-12-04 15:10:56 24 4
gpt4 key购买 nike

MVC 6 如何渲染 View 。 Razor ViewEngine 中生成 html 输出的实际方法是什么?如果可能的话,请解释渲染 View 的过程。

也许您可以将我指向 github 上 mvc 源的文件。谢谢!

最佳答案

这是您正在寻找的完整解决方案。我使用依赖注入(inject)来获取 Controller 中的 HtmlHelper。如果你愿意,你也可以注入(inject)你自己的助手。

using Microsoft.AspNet.Html.Abstractions;
using Microsoft.AspNet.Mvc;
using Microsoft.AspNet.Mvc.ModelBinding;
using Microsoft.AspNet.Mvc.Rendering;
using Microsoft.AspNet.Mvc.ViewEngines;
using Microsoft.AspNet.Mvc.ViewFeatures;
using Microsoft.AspNet.Mvc.ViewFeatures.Internal;
using Microsoft.Extensions.WebEncoders;
using System.ComponentModel.DataAnnotations;
using System;

public class MyController : Controller
{
private readonly IHtmlGenerator htmlGenerator;
ICompositeViewEngine viewEngine;
IModelMetadataProvider metadataProvider;
private readonly IHtmlHelper helper;
IHtmlEncoder htmlEncoder;
IUrlEncoder urlEncoder;
IJavaScriptStringEncoder javaScriptStringEncoder;

public MyController(IHtmlHelper helper, IHtmlGenerator htmlGenerator, ICompositeViewEngine viewEngine, IModelMetadataProvider metadataProvider, IHtmlEncoder htmlEncoder, IUrlEncoder urlEncoder, IJavaScriptStringEncoder javaScriptStringEncoder)
{

this.htmlGenerator = htmlGenerator;
this.viewEngine = viewEngine;
this.metadataProvider = metadataProvider;
this.htmlEncoder = htmlEncoder;
this.urlEncoder = urlEncoder;
this.javaScriptStringEncoder = javaScriptStringEncoder;
this.helper = helper;
}

[HttpGet]
public IActionResult MyHtmlGenerator()
{
MyViewModel temp = new MyViewModel();


var options = new HtmlHelperOptions();
options.ClientValidationEnabled = true;

ViewDataDictionary<MyViewModel> dic = new ViewDataDictionary<MyViewModel>(this.metadataProvider, new ModelStateDictionary());

ViewContext cc = new ViewContext(ActionContext, new FakeView(), dic, TempData, TextWriter.Null, options);

var type = typeof(MyViewModel);
var metadata = this.metadataProvider.GetMetadataForType(type);


ModelExplorer modelEx = new ModelExplorer(this.metadataProvider, metadata, temp);
ViewData["Description"] = "test desc";
ViewData["Id"] = 1;

this.ViewData = new ViewDataDictionary(this.metadataProvider, new ModelStateDictionary());

IHtmlHelper<MyViewModel> dd = new HtmlHelper<MyViewModel>(this.htmlGenerator, this.viewEngine, this.metadataProvider, this.htmlEncoder, this.urlEncoder, this.javaScriptStringEncoder);
((ICanHasViewContext)dd).Contextualize(cc);
dd.ViewContext.ViewData = this.ViewData;

var desc = GetString(dd.TextBoxFor(m => m.ID));
var ID = GetString(dd.TextBoxFor(m => m.Description));

// Do whatever you want with the ID and desc

return new ContentResult() { Content = ID + desc };

}

public static string GetString(IHtmlContent content)
{
var writer = new System.IO.StringWriter();
content.WriteTo(writer, new HtmlEncoder());
return writer.ToString();
}
}


public class MyViewModel : BaseAssetViewModel
{
// [RegularExpression(@"^-?\d{1,13}(\.\d{0,5})?$|^-?\.\d{1,5}$")]
[Required]
public int ID { get; set; }

[MinLength(2)]
public string Description { get; set; }

// Property with no validation
public string Other { get; set; }
}


public class FakeView : IView
{
string IView.Path
{
get
{
throw new NotImplementedException();
}
}

public Task RenderAsync(ViewContext viewContext)
{
throw new InvalidOperationException();
}

Task IView.RenderAsync(ViewContext context)
{
throw new NotImplementedException();
}
}

关于razor - Asp.net Core 如何渲染 View ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27902545/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com