Shaokan
Shaokan

Reputation: 7684

webmatrix c# and razor

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.

Upvotes: 2

Views: 2443

Answers (3)

pejman
pejman

Reputation: 1

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)

Upvotes: -1

simtan
simtan

Reputation: 364

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.

Upvotes: 1

Chris Cap
Chris Cap

Reputation: 1068

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

<td>@Model.Name</td>
<td>@Model.Description</td>

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.

Upvotes: 2

Related Questions