可以将文章内容翻译成中文,广告屏蔽插件可能会导致该功能失效(如失效,请关闭广告屏蔽插件后再试):
问题:
I think Microsoft must have a reason for enhancing ASP.Net with RAZOR syntax.
On the Create New Website Project dialog of visual studio, there is another option for creating ASP.Net (Razor). The first time I came across the term Razor was when I read a book on Asp.Net MVC, I didn't know it exists for ASP.Net
I know what the Razor syntax is for, introduced in MVC 3. Before asking this question, I decided to create a test project for ASP.Net (Razor) and see how it is different from the normal ASP.Net webforms and ASP.Net MVC. I discovered no Model/View/Controllers folders like we have in MVC.
I discovered that the Master page is specified differently and the Master page is cleaner, no more <asp:ContentPlaceHolder />
But my questions are:
- I notice it uses
.cshtml
, are there no more codebehinds?
- similarities and differences between ASP.Net (Razor) and ASP.Net MVC
- Can it be extended to function like ASP.Net MVC e.g. adding Controller?View/Model, Custom Routing I guess?
- Why does this exist when we have ASP.Net MVC? Wouldn't this encourage reluctance to move to ASP.Net MVC?
- and why would you choose ASP.Net+Razor over ASP.Net MVC?
UPDATE: ASP.NET Web Pages in particular was designed to make it easy for
people who already know HTML to add server processing to their pages.
It's a good choice for students, hobbyists, people in general who are
new to programming. It can also be a good choice for developers who
have experience with non-ASP.NET web technologies
Update ASP.NET Web Pages ASP.NET Web Pages targets developers who want a simple web development story, along the lines of PHP. In the Web Pages model, you
create HTML pages and then add server-based code to the page in order
to dynamically control how that markup is rendered. Web Pages is
specifically designed to be a lightweight framework, and it's the
easiest entry point into ASP.NET for people who know HTML but might
not have broad programming experience — for example, students or
hobbyists. It's also a good way for web developers who know PHP or
similar frameworks to start using ASP.NET.
Asp.Net Web Pages
Please, I need your technical opinion.
Thanks.
回答1:
This is a great question. First, lets characterize Razor.
Razor is an engine that parses server-side code an emits Html, just like ASP.NET Web Forms only with different and arguably more streamlined and terse syntax.
Razor v. Web Forms Sidebar: In ASP.NET Web Forms you have to identify when you wanted to start writing server code with '<%' and then when you were done writing server code you needed to identify that with '%>'. I love ASP.NET Web Forms, but that's clunky. With Razor you identify when you want to start writing server code with '@' and then the next time you start writing a server tag (starting with '<') it "figures out" that you're done with server code. It's a more concise way to write html intermingled with some server code.
ASP.NET Web Pages is a framework for creating simple Web Applications. ASP.NET MVC is a framework for creating web applications with either the Web Forms or Razor engine using the Model-View-Controller (MVC) pattern. ASP.NET Web Forms is a framework for creating web applications using the Web Forms render engine.
Ultimately the goal is to provide choice based on the sophistication of the application that is being built. Understanding each with assist you in making the correct choice for your application.
Additional Links:
- ScottGu's Blog - Introducing Razor
- ScottGu's Blog - Introducing WebMatrix
- ASP.NET Web Page with Razor Syntax (MSDN)
回答2:
In the MVC pattern, M is the Model, C is the Controller, and V is the View. So, quite naturally, in the ASP.NET MVC model, there is the concept of a View Engine. Razor is simply one of the View Engines provided. The other one provided out-of-the-box is the "old" WebForms one (you can also write your own View Engine by the way). So Razor does not have the notion of code-behind which stays within the WebForms view engine boundaries.
So this kinda says it all. Razor handles the View part of MVC (If you choose to use it instead of the WebForms one). It has nothing to do with M or C.
Personally, I would definitely go for the Razor View Engine if you choose the ASP.NET MVC pattern, or use plain WebForms without MVC, as Razor has been designed to be less verbose, more simple to use than the Webforms one. It's also simply more recent so it tries to be ... simply better :-)
As a side note, the Razor Parser can also be used outside of ASP.NET MVC. It's implemented in an assembly that does not rely on MVC nor Web assemblies at all. See here for more on this: http://www.west-wind.com/weblog/posts/2010/Dec/27/Hosting-the-Razor-Engine-for-Templating-in-NonWeb-Applications
回答3:
There are not code-behinds by default, but you can easily make your razor file inherits from your custom class:
@inherits Index
and then
public class Index : WebViewPage { }
(More information here: http://www.compiledthoughts.com/2011/01/aspnet-mvc3-creating-razor-view-engine.html)
This is more like an old-fashioned way of doing web, more asp3-like. The difference with mvc is that mvc provides a huge framework that supports real world applications (using routing, controller and actions, and not just "code there in the markup").
I think it exists for making things that are really simple, but I don't actually know...
Finally, I would always chose asp.net mvc with razor.
Hope it helps
回答4:
Well, You have two options:
Use WebForms: use out-of-the box, ready to use server-side controls(mixed markup and business codes) and use master pages and skins, but face some complexity involved of these conveniences! :)
Use MVC: or use separated design model which gives you a more organized codebase. you can first make a design prototype, or first making a business codes, then building the other aspect, quite easy. even you can give the designer more control on his work, giving him/her the ability to do everything he/she wants.
-> this one is my preferred choice because it gives me more control on my code, makes my code more concise and clean.
If you selected the MVC pattern, then you are faced with another options... View Engines
- Old MVC View Engine: it is not bad, but is a bit verbose
- Razor Engine: it does the same thing the #1 does, why you ever don't want to use it?! it replaces nothing of the MVC pattern. it is also more concise and easy, you have both the power of MVC and the simplicity of the Razor Engine. Razor is a smarter in-markup syntax, which helps you stay focused on your business, rather than writing "<%"s !
by using razor syntax you are provided just a benefit, the full power of MVC is at your fingertips!
You can also write simple asp.net web pages just with razor(C# or VB) syntax. (just like php)
- My personal choice would be MVC+Razor, The best combination!
回答5:
- There are no "code-behinds" in MVC, period. What you have are controllers, which exist both for Razor and non-Razor.
- They're just two different types of markup. (See next answer)
- It's not replacing MVC, it's just another option. The traditional markup is very verbose. Razor syntax is succinct.
回答6:
To get out of all the confusion by webform, MVC, Razor, WebPages and lot more to come from microsoft, I prefer html-->javascript-->webapi.