MVC source code analysis, mvc source code

Source: Internet
Author: User

MVC source code analysis, mvc source code

After the year, everyone is busy, and I am no exception. however, I am not busy looking for a job in an interview, but busy with my current job. I don't have to talk much about it. It's the topic of today.

I. Where is the Index page?

It is strange that no view details have been seen in the dll files under the directory bin. So where are they?

First, add a sentence in the Index page: @ this. GetType (). Assembly. Location

After the program runs, you can view the running directory on the page.

My directory is this:

C: \ Users \ xxxxx \ AppData \ Local \ Temp \ Temporary ASP. NET Files \ vs \ 31cf6113 \ 79f9587c \ App_Web_xwp5b05u.dll

The section marked as yellow is a random number, so you don't have to worry about it. Get it and decompile it directly to see the effect:

[Dynamic(new bool[] { false, true })]public class _Page_Views_Home_Index_cshtml : WebViewPage<object>{    // Methods    public override void Execute()    {        ((dynamic) base.ViewBag).Title = "Index";        base.BeginContext("~/Views/Home/Index.cshtml", 0x23, 4, true);        this.WriteLiteral("\r\n\r\n");        base.EndContext("~/Views/Home/Index.cshtml", 0x23, 4, true);        base.BeginContext("~/Views/Home/Index.cshtml", 40, 0x20, false);        this.Write(base.GetType().Assembly.Location);        base.EndContext("~/Views/Home/Index.cshtml", 40, 0x20, false);        base.BeginContext("~/Views/Home/Index.cshtml", 0x48, 0x33, true);        this.WriteLiteral("\r\n\r\n

From this point, the common tag statements on the page are parsed into strings through the this. WriteLiteral () method, and @ Model. Name is also parsed.

I noticed that the Model is converted into a dynamic variable. Can I pass a dynamic value in the View? The demo will be provided later.

In the System. Web. Mvc. RazorView. RenderView () method in the previous article, there is a sentence: WebPageRenderingBase startPage = null;

The _ Page_Views_Home_Index_cshtml class is indirectly inherited from the WebPageRenderingBase class.

 

Ii. Basic Razor usage

In MVC, on the front-end page, you can use Razor to program as in the background. Then, use the @ variable to output the Code. For example:

@if (true){    <table>        <tr>            <td>@Model.Name</td>        </tr>        <tr>            <td>@Model.Age</td>        </tr>    </table>}

In the brackets, you can directly write html because the compiler can recognize tags and know how to parse them.

So if I write this in the <script> tag, for example:

@if (true){    alert(1);}

This is not acceptable, because there is no obvious mark for the compiler to identify. The alert (1) in it belongs to the html or the C # statement. It cannot be recognized.

At this time, you need to use other things to help identify. Two Methods: 1. <text> </text> 2 .@:

<script>    @if (true)    {        @: alert(1);        <text>alert(2);</text>    }    else    {        @: alert('@DateTime.Now.ToString()');    }</script>

These two methods are acceptable. Let's look at my habits. When using <text>, the indentation of vs makes people look uncomfortable, just like the following:

<script>    @if (true)    {        @: alert(1);        <text>    alert(2);    </text>    }    else    {        @: alert('@DateTime.Now.ToString()');    }</script>

The two methods have their own advantages and disadvantages. <text> </text> can contain a lot of content in the middle. @: when multiple rows are encountered, each line must be added.

 

3. Data passing through Model

In View (model), the backend variables can be passed. Where is the model stored?

public TModel Model{    get    {        return this.ViewData.Model;    }}

Here, it is stored in ViewData. That is to say, I can use ViewData. Model in the background to replace the View (model) method to pass the value. In fact, it is also possible.

The values of the general type can be passed. As mentioned above, can the dynamic value be passed?

The answer is that it cannot be passed directly. That is to say, I cannot pass the following message: return View (dynamic). Can I pass the anonymous type? return View (new {name = "haha "})?

The answer cannot be passed directly.

Is there a way to pass these values? Of course. Otherwise, I won't ask that question.

Demo1:

public ActionResult Index(){    dynamic model1 = new ExpandoObject();    model1.Name = "ExpandoObject";    model1.Age = 12;    var res =   JsonConvert.DeserializeObject(JsonConvert.SerializeObject(model1));    return View(res);}

The Index page is the one above. Check the result:

Here, I will serialize and deserialize it. model1 here can also be anonymous.

However, this method is not recommended, and it is quite troublesome. There is an additional serialization and deserialization.

 

Directory synchronized

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.