剃刀 – Asp.net Core如何呈现视图
发布时间:2020-12-16 03:54:01 所属栏目:asp.Net 来源:网络整理
导读:MVC 6如何呈现视图. Razor ViewEngine中生成html输出的实际方法是什么?如果可能的话,请解释渲染视图的过程. 也许你可以指点我在github上的mvc源文件.谢谢! 解决方法 这是您正在寻找的完整解决方案.我使用依赖注入来获取控制器中的 HtmlHelper.如果你愿意的
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-mvc-3 – 如何通过ajax上传文件
- asp.net-core – 当返回null而不是控制器中设置的值时,Mode
- asp.net-mvc-3 – MVC3 Html.ActionLink Post
- asp.net-mvc – 如何在不同的控制器中管理MVC区域和RenderA
- asp.net – 返回新的RedirectResult()vs返回Redirect()
- git/sourcetree解决本地仓库历史合并到线上仓的历史数据合并
- asp.net-mvc – ASP.NET MVC路由和静态数据(即图像,脚本等)
- asp.net – SQL网络接口,错误:26 – 定位服务器/实例指定错
- asp.net core 入口程序
- asp.net – 在EF中的自联接表中选择Last Childs