[Asp.net mvc odd tricks] 02, asp. netmvc02

Source: Internet
Author: User

[Asp.net mvc odd tricks] 02, asp. netmvc02

In web development, Html is often obtained in internal code, which needs to be rendered together with data. It does not directly return the Html code to the client. This method has many application scenarios, such as paging, Ajax obtaining several Html segments at a time, generating email sending templates, and generating Html static pages. The simple or easy-to-think method is to directly splice Html. Of course, this is definitely not the most appropriate method.

Application Scenario 1. One method in paging is to use ajax to obtain the html code of the table and Json of some paging information.
var json = {    "table": "<table><tr/><td>1</td></tr></table>",    "pageSize": 10,    "currentIndex": 1,    "count": 100}

 

2. Ajax obtains several Html segments at a time.
var json = {    "leftHtml": "<div>

 

3. generate an email sending template and Html static page

We often generate some email templates, such as html code to promote 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 splicing it into json or sending an email, or generating a static html page.

Generating Html is no more than the Razor engine in asp.net. In short, it is very useful and has powerful syntax. If we use the Razor engine to generate the html we need, isn't it good, if you are familiar with the asp.net mvc pipeline, you can easily solve this problem.

1. Search for View (cshtml)

You can use ViewEngines. Engines. FindView to find the View.

public virtual ViewEngineResult FindView(ControllerContext controllerContext, string viewName, string masterName);

FindView requires ControllerContext, viewName, and masterName. masterName is the name of the master view, which can be ignored currently.

ViewName is the View we need to find. The way to find the View is the same as the way to return View (string viewName) in Action. That is to say, there are two ways: full path, such: "~ /Views/Home/Html1.cshtml ", must contain the suffix cshtml. Another way is to directly write "Html1", that is, the relative path. if the location of the cshtml file is not in the folder corresponding to the Controller, you can write ".. /Folder/Html1 ". This method also applies to the normal execution of the Controller's direct return View (string viewName) Action ).

ControllerContext encapsulates information about the specified System. Web. Routing. RouteBase and System. Web. Mvc. ControllerBase requests.

public ControllerContext(RequestContext requestContext, ControllerBase controller);

In the constructor, RequestContext and ControllerBase are required. ControllerBase is this, and RequstContext can be easily obtained in Action.

Final View code

ControllerContext context = new ControllerContext(Request.RequestContext, this);ViewEngineResult result = ViewEngines.Engines.FindView(context, "Html1", "");

 

2. Render View

In the end, we need to execute the Render method of View to obtain the generated html

void Render(ViewContext viewContext, TextWriter writer);

Render code

using (var sw = new StringWriter()){    var viewContext = new ViewContext(context, result.View, context.Controller.ViewData, context.Controller.TempData, sw);    result.View.Render(viewContext, sw);    string html = sw.ToString(); }

In the code, html is the html we need to obtain.

 

Transfer Data to View

How to pass data to the View, which is consistent with the normal Action execution, that is, we are familiarViewBag, ViewData, TempData, and ModeL can be used.

1. Set Data

Set the data before calling View. Render.

Context. controller. viewBag. name = "Emrys"; context. controller. viewData ["Age"] = 10; context. controller. tempData ["City"] = "Shanghai"; context. controller. viewData. model = new UserInfo {Name = "Emrys", Age = 10, City = "Shanghai "};

 

2. obtain data in View (html), that is, the Razor code in Html1.cshtml.
@{    Layout = 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

 

Summary

Code in the final Action

ControllerContext context = new ControllerContext (Request. requestContext, this); ViewEngineResult result = ViewEngines. engines. findView (context, "Html1", ""); context. controller. viewBag. name = "Emrys"; context. controller. viewData ["Age"] = 10; context. controller. tempData ["City"] = "Shanghai"; context. controller. viewData. model = new UserInfo {Name = "Emrys", Age = 10, City = "Shanghai"}; using (var sw = new StringWriter ()) {var viewContext = new ViewContext (context, result. view, context. controller. viewData, context. controller. tempData, sw); result. view. render (viewContext, sw); string html = sw. toString ();}

In this way, we can use Razor to obtain the html code that we need to combine with the data for our use.

I hope it will be helpful to you. This article is original and you are welcome to make a brick andRecommendation.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.