webmatrix C# 和 razor
我想尝试一下razor,但网上的教程不多;我已经尝试过
请注意:我没有使用 Visual Studio,我使用的是 webmatrix。
I wanted to give a try to razor but there are not many tutorials on web; I've already tried this one, so better ask here. Can anyone please tell me how razor works? I mean are there any code behind pages or controls? and what is done in code behind pages if they exist? Also, how can I reference code behind pages? Generally speaking, what type of code fits in the cshtml page, I mean, for the sake of a clear design how should be the structure etc etc. finally, any good tutorial is highly appreciated. Thanks!
Please note: I'm not using visual studio, I'm using webmatrix.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
我无法对 WebMatrix 说太多。但是...您熟悉 ASP.NET MVC 吗?鉴于您所问的问题,听起来也许您不是。我首先会学习 MVC 的基础知识。我在 NerdDinner 示例中学习了
http://weblogs.asp.net/scottgu/archive/2009/03/10/free-asp-net-mvc-ebook-tutorial.aspx
更直接地回答你的问题,不,文件后面没有代码(尽管从技术上讲,后面的代码可能是通过一些黑客行为实现的)。该范例与网络表单完全不同。本质上,您有称为控制器的类,用于累积页面的数据。页面的“数据”被称为模型,并且与您的问题域非常相似。模型是用于表示系统中的对象(客户、用户、订单、小部件等)的类。控制器将模型传递给视图。视图呈现页面。因此,您期望在其中每个代码中找到的代码是:
模型:代表问题域的对象,如博客、个人、用户等......
控制器:使用返回模型对象的服务从数据库获取数据(或调用实体框架、NHibernate 等)
视图:使用 cshtml、aspx 或任何 ViewRenderer 进行显示。
因此,在您的视图 (cshtml) 中,您实际上只需进行简单的编程即可显示 UI 元素并执行 UI 逻辑。您将主要拥有 HTML,然后散布一些位以从模型中提取数据并显示它,
希望这有助于
编辑:在阅读更多内容后,听起来 WebMatrix 是一种用于 Web 应用程序的 RAD 工具。看看您发布的教程,看起来他们正在向您展示像经典 ASP 一样对待它,并将所有内容都放在 cshtml 页面上。这是一个选项。但我不会推荐它。这会让生活变得艰难,但我不确定 WebMatrix 是否有更好的方法。
I can't speak a whole lot to WebMatrix. But...are you familiar with ASP.NET MVC? Given what you're asking, it sounds like maybe you aren't. I would start with learning the basics of MVC first. I learned on the NerdDinner example
http://weblogs.asp.net/scottgu/archive/2009/03/10/free-asp-net-mvc-ebook-tutorial.aspx
To answer your question more directly, no there are no code behind files (though a code behind is technically possibly through some hackery). The paradigm is completely different from web forms. Essentially you have classes called controllers that accumulate the data for a page. The "data" for the page is referred to as a model and closely resembles what your problem domain is. Models are classes intended to represent objects in the system (customers, users, orders, widgets, etc...). The controller passes models to the view. The view renders the page. So the code you'd expect to find in each of these is:
Model: object representing the problem domain like Blog, Person, User, etc...
Controller: Gets data from database using services that return Model objects (or calls into entity framework, NHibernate, whatever)
View: displays that using cshtml, aspx or whatever the ViewRenderer is
So, in your view (cshtml), you're really just going to have straightforward programming for displaying UI elements and performing UI logic. You'll have mostly HTML and then some bits scattered around to pull data from the model and display it like
Hope that helps
EDIT: After reading a bit more, sounds like WebMatrix is kind of a RAD tool for web apps. Looking at the tutorial that you posted, looks like they're showing you go treat it like classic ASP and put everything right on the cshtml page. This is an option. I wouldn't recommend it though. It'll make life tough, but I'm unsure if there are better ways in WebMatrix.
Razor 是一个视图引擎,可以在 Razor 页面 (.CSHTML/.VBHTML) 或 ASP.NET MVC 中单独使用。如果您单独使用它,这有点像编写经典的 ASP 或 PHP,其中代码都是内联的。没有隐藏代码。下面简单介绍一下语法:
http://www.mikesdotnetting.com /Article/153/Inline-Razor-Syntax-Overview
该网站还有许多其他有关 Razor 本质的精彩文章。
Razor is a view engine, which can be used by itself in Razor pages (.CSHTML/.VBHTML) or in ASP.NET MVC. If you use it by itself, it's kind of like writing classic ASP or PHP, where the code is all inline. No code-behinds. Here's a brief introduction to the syntax:
http://www.mikesdotnetting.com/Article/153/Inline-Razor-Syntax-Overview
That site also has a lot of other great articles about the nature of Razor.
创建App_Code文件夹将cs文件放入此文件夹
将App_Code转换为bin文件夹中的dll然后上传bin文件夹
(由 Visual Studio 2012/发布网站)
create App_Code folder put cs file to this folder
convert App_Code to dll in bin folder then upload bin folder
(by visual studio 2012/publish web site)