剃刀 – Asp.net Core如何呈现视图

剃刀 – Asp.net Core如何呈现视图,第1张

概述MVC 6如何呈现视图. Razor ViewEngine中生成html输出的实际方法是什么?如果可能的话,请解释渲染视图的过程. 也许你可以指点我在github上的mvc源文件.谢谢! 这是您正在寻找的完整解决方案.我使用依赖注入来获取控制器中的 HtmlHelper.如果你愿意的话,你可以注入自己的助手. using Microsoft.AspNet.Html.Abstractions;us MVC 6如何呈现视图. Razor VIEwEngine中生成HTML输出的实际方法是什么?如果可能的话,请解释渲染视图的过程.

也许你可以指点我在github上的mvc源文件.谢谢!

解决方法 这是您正在寻找的完整解决方案.我使用依赖注入来获取控制器中的 HtmlHelper.如果你愿意的话,你可以注入自己的助手.

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();    }}
总结

以上是内存溢出为你收集整理的剃刀 – Asp.net Core如何呈现视图全部内容,希望文章能够帮你解决剃刀 – Asp.net Core如何呈现视图所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/langs/1239654.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-06-06
下一篇 2022-06-06

发表评论

登录后才能评论

评论列表(0条)

保存