Asp.net Core如何呈现视图 [英] How does Asp.net Core renders a view

查看:252
本文介绍了Asp.net Core如何呈现视图的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

MVC 6如何呈现视图. Razor ViewEngine中生成html输出的实际方法是什么?另外,请尽可能说明渲染视图的过程.

How does MVC 6 renders a view. What's the actual method in Razor ViewEngine that generates the html output? Also if possible please explain the process of rendering a view.

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

May be you could point me to a file on mvc source on github. thanks!

推荐答案

以下是您正在寻找的完整解决方案.我使用依赖注入在控制器中获取HtmlHelper.您也可以根据需要注入自己的助手.

Here is a complete solution of what you are looking for. I used dependency injection to get the HtmlHelper in the controller. You can inject your own helper if you want too.

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如何呈现视图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆