2015-01-12 9 views
6

In che modo MVC 6 esegue il rendering di una vista. Qual è il metodo effettivo in Razor ViewEngine che genera l'output html? Se possibile, spiega anche il processo di rendering di una vista.In che modo Asp.net Core esegue il rendering di una vista

Potrebbe essere possibile indicarmi un file sul sorgente mvc su github. Grazie!

+0

Ho paura che questo è un po 'troppo ampia per rispondere, ma fondamentalmente un ViewResult ('Return()') compila la vista appropriata utilizzando il compilatore C# on-the-fly. Non penso che questo sia documentato ovunque tranne che nella fonte stessa. Cosa ti impedisce di scaricare l'albero dei sorgenti e trovare ciò che stai cercando? Che cosa stai cercando di fare esattamente, perché vuoi saperlo? – CodeCaster

+0

Sto provando a creare il mio htmlHelper al volo attraverso il codice e quindi la visualizzazione Render a seconda del modello fornito. Sto osservando anche il codice sorgente, ma sembra difficile da trovare senza documentazione. – eadam

+0

Questa mira sembra un po 'indietro. Sicuramente il controllore potrebbe/dovrebbe prendere la decisione. In realtà intendi viste parziali? –

risposta

2

Ecco una soluzione completa di ciò che stai cercando. Ho usato l'iniezione di dipendenza per ottenere HtmlHelper nel controller. Puoi iniettare il tuo aiuto se lo vuoi anche tu.

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

Non so se questo può essere di aiuto, può essere bisogna iniziare a guardare aiutanti tag:

https://github.com/DamianEdwards/TagHelperStarterWeb

che stanno lavorando a un modo diverso per creare aiutanti che integrano nella pagina in un modo più naturale.

Problemi correlati