In web development, it is common to get HTML in internal code that needs to be rendered together with the data. The HTML code is not returned directly to the client. This approach has many scenarios, such as paging, Ajax fetching a few pieces of HTML fragments at a time, generating mail-sending templates, generating HTML static pages, and so on. The simple or easy-to-think approach is to directly splice HTML, which is certainly not the most appropriate approach.
Scenario 1, in pagination, one approach is to use AJAX to get the HTML code of the table and some of the JSON for paging information
var json = { "table": "<table><tr/><td>1</td></tr></table > ", " pageSize ": Ten, " Currentindex ": 1, " Count ":
2, Ajax one-time access to a few pieces of HTML fragments
var json = { "lefthtml": "<div>, "righthtml": "<table><tr/><td>1</td></tr></table>"}
3, generate the mail send template, generate HTML static page
We often generate some mail templates, such as the HTML code that promotes some products.
Generating HTML static pages is more common.
Application Scenario Analysis
All of our applications generate HTML in the internal code and then process the HTML code, such as stitching it into JSON, or sending a message, or generating a static HTML page.
HTML generated in the ASP is no more than the razor engine, in short, is very good, the syntax is very powerful, if we need to generate HTML with the Razor engine generated is not very good, if familiar with the ASP. NET MVC pipeline can be very simple to solve this problem.
1. Find View (cshtml)
You can use ViewEngines.Engines.FindView to find the view.
Public Virtual string string mastername);
Findview requires ControllerContext, viewname, and Mastername, where Mastername is the name of the master view that can now be ignored.
ViewName is the view that we need to find, the way to find the view is consistent with the return view (String viewName) in action, which means that there are two ways, one is the full path, such as: "~/views/home/ Html1.cshtml ", must be cshtml with the suffix name. Another way is to directly write "HTML1", that is, the relative path, if the location of the cshtml file is not in the controller corresponding folder, you can write ". /folder/html1 ". This approach also adapts to the normal execute controller in the action direct return View (string viewName).
ControllerContext is encapsulating information about the request with the specified System.Web.Routing.RouteBase and System.Web.Mvc.ControllerBase
Public ControllerContext (RequestContext RequestContext, controllerbase Controller);
The need for RequestContext and controllerbase,controllerbase in constructors is that This,requstcontext can be easily obtained in action.
The code that eventually looks for the view
New This "Html1" "");
2. Render View
Finally we need to execute the render method of view to get the generated HTML
void Render (ViewContext ViewContext, TextWriter writer);
Render Code
using (varnew StringWriter ()) { varnew viewcontext (context, result. View, context. Controller.viewdata, context. Controller.tempdata, SW); Result. View.render (ViewContext, SW); string html = SW. ToString (); }
The HTML in the code is the HTML we need to get.
Pass data to view
How to pass the data to view, which is consistent with the normal action, which means that we are familiar with the viewbag,viewdata,tempdata and modeL can be used.
1. Set the data
The data can be set before calling View.render.
Context. Controller.ViewBag.Name ="Emrys"; context. controller.viewdata[" Age"] =Ten; context. controller.tempdata[" City"] ="Shanghai"; context. Controller.ViewData.Model=NewUserInfo {Name ="Emrys", age =Ten, City ="Shanghai"};
2. Get the data in view (HTML), that is, the razor code in html1.cshtml.
@{ null;} @model UserInfo name: @ViewBag. Name<br/> Age: @ViewData [ 'age']<br/> City: @TempData ['city']<br/ >Name: @Model. Name<br/> Age: @Model.age <br/> City: @Model. City
Summarize
The code in the final action
ControllerContext context =NewControllerContext (Request.requestcontext, This); Viewengineresult result= ViewEngines.Engines.FindView (Context,"HTML1",""); context. Controller.ViewBag.Name="Emrys"; context. controller.viewdata[" Age"] =Ten; context. controller.tempdata[" City"] ="Shanghai"; context. Controller.ViewData.Model=NewUserInfo {Name ="Emrys", age =Ten, City ="Shanghai" };using(varSW =NewStringWriter ()) { varViewContext =NewViewContext (context, result. View, context. Controller.viewdata, context. Controller.tempdata, SW); Result. View.render (ViewContext, SW); stringHTML =SW. ToString (); }
So we can use razor to get the HTML code we need and the data combination for us.
Finally, I hope you have some help, this article is original, welcome to make bricks and recommend .
[ASP] 02-Use the Razor engine to generate HTML code within an action