Idle language mvc3 and razor

Source: Internet
Author: User

I didn't have this blog until I saw this article from scottgu.ArticleASP. net mvc 3: Razor's @: and <text> syntax. I decided to record some of my views on mvc3 and razor over the past few days.

About razor

Recently, my friends and blog friends seem to have little affection for mvc3 and razor, and even think that Microsoft is making another mistake in developing razor. Although I have not actually used razor in the project, I have been paying close attention to the progress of razor for a long time. I also know the syntax of razor, and the overall feeling is that it is cleaner and smoother, others do not have a deeper understanding. When reading the scottgu article at nightCodeThe clip immediately attracted me:

Do you know where this code attracts me? Yes, that is, the combination of C # code and HTML is very smooth (especially in the processing of curly brackets, it really makes my eyes shine, because currently the aspx code with curly braces is the most annoying), the corresponding aspx syntax is as follows:

Matching two code snippets is very attractive to me at least two points:

    1. Aspx syntax, a complete foreach code segment, is split by several different segments. In terms of writing, we can use code snippet to quickly generate the foreach code format. However, due to aspx Syntax problems, we need to constantly move the cursor position to split the curly brackets, the HTML code to be output is placed in the middle. In terms of reading, I have to use the third eye to count <%> (in fact, I don't have to, but I have to make the original two take more than one ).
    2. I don't know if you have noticed that C # codes of the same code block are highlighted. In the aspx code, we cannot clearly distinguish which code blocks are related.

In fact, I can imagine from these two codes that there are several layers of nesting in our code in their respective scenarios. As aspx is currently the main (almost unique) template engine, I am almost used to it. I need to open my eyes to match the matching relationship between curly braces, especially when our code exceeds one screen, it is a pleasure.

After reading scottgu's entire article, I learned about Razor's best practices after I have done a few new experiments. When razor processes C # code, it starts with the code snippet starting with @ (if .. else, for, foreach, or empty statement @ {} code block) and other statements, from {start, to the end of the corresponding}, are all areas where C # code can be written, we can write any C # code without any additional identifier until we encounter:

    1. @ (In this case, @ Can Be Used to Output C # VARIABLES or attribute values ).
    2. @: Used to output static strings.
    3. HTML tags are output directly. Areas in HTML tags do not belong to the C # code area. C # Code cannot be written. Files in HTML tags are output directly. @ is required for output attributes @.
    4. Text text blocks, text blocks also exist in the form of HTML tags, but they are not standard HTML tags, but they will be specially processed by razor and only output the text in the text, but its own labels will not be output. This may be useful when we need to output multiple lines of text.

Here, I would like to make a special note. I don't know if you have encountered this problem. In aspx, if I want to bind the C # variable to the tag property, I cannot use the code prompt in it. I often want to write a piece of code in the external header, and then copy it in. This makes me very painful, even if I can write that code manually. Please forgive me for my low level. I am not the kind that I can write with notepad.ProgramProgrammer. If you have the same pain as me, razor will surprise you and look at the truth:

@ Is the soul of razor. All syntaxes start or end with @, @ using, @ model ,@**@,@:. I know this now, but it's hard to imagine that everything in razor is easier than that in Aspx. Although @ ** @ doesn't look much better than <% -- %>, however, it is more personalized. Even the icons in the razor file are @ characters.

As for the nature of the template, Razor itself is still driven by server code, rather than the restrictive markup syntax. You can write any code in the view. Security needs to be considered. However, in terms of flexibility, I believe there are still many programmers who cannot leave free templates. Although restricted templates can achieve the same purpose. I have been wondering whether there is a way to independently restrict the access permissions of the Code in the view, similar to the trust level, but this trust level is used to restrict the code in the template.

About mvc3

With regard to mvc3, I personally do not deny that my follow-up attitude is a little radical, so that I will upgrade to mvc3 R2 if it will affect other projects. In fact, when R1 came out, I tried to upgrade it, but gave up because of a bug that was difficult to bypass. I was not so eager to upgrade to mvc3 R2, but on Monday, when my colleagues introduced mvc3, the unobtrusive js I mentioned suddenly reminded me that, can I use this feature to break through the difficulties we encountered that day. In my blog that day ago, I also mentioned that I didn't like unobtrusive JS very much at the time. Just a week later, I now like unobtrusive JS very much, it is really clean and easy to use, and it is also very convenient to troubleshoot. Although many people will say that unobtrusive JS was not invented by Microsoft, but I would like to say that using modelmetadata on the server side and unobtrusive JS on the client side can make everything much simpler, it is easy to improve the architecture without performance loss.

MVC is now in version 3. As a developer who has been tracking the beta version and used for actual project development, I understand MVC, although not as deep as @ Jeffery Zhao, relatively speaking, familiar with the entire MVC framework. I have also realized that Microsoft products (or products of any company) will be mature and easy to use from the third version. Although I agree with this argument, it is undeniable that the MVC project followsCommunityOpen development path. The ASP. NET team obtained comments from the community through a large number of CTP, beta, and RC versions from the very beginning, which is really good and makes the product easier to use. Although I did not make any contribution directly, I learned more and faster from the changes of these versions.

In mvc1, mvc1 is still regarded as a JSON service in a large number. Almost 95% of Controller/action statements return JSON objects. In Code involving views, I also habitually use webform, user controls, page base classes, and server controls, which are almost useless. However, even if I used the MVC framework at the time to complete a standard web development process, it seems that it may be quite poor.

After modelmetadata is added to MVC2, the MVC framework is upgraded to another level. Combined with HTML. XXXFor and dataannotation, it makes form generation, data binding, and data verification easier. With modelmetadata MVC, the MVC framework enters another stage.

Relatively speaking, I think mvc3 continues to be improved based on MVC2 and some "look" Features of. Net 4. However, it requires. net4. Apart from the dynamic feature, it cannot be used in. Net 3.5. Other features can of course be extracted separately from mvc3 of basic. Net 3.5. In addition, do not be cheated. viewbag is not very commonly used, or is not particularly useful.

For mvc3, I also want to say that I really like it. I'm looking forward to razor.

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.