18

I'm not new to programming but am new to Visual Studio, MVC, C#, ASP.NET, and EXT.NET, i.e. all of the tools I am trying to use.

I am trying to create an Ext.Net 2.0 MVC4 project and was given a similar (in functionality) non-MVC project for reference.

I see that this non-MVC project has .aspx and .aspx.cs files. It seems like the .aspx file basically maps to the "View" in MVC that I want to make... And the .aspx.cs file has the functions that relate to the .aspx file - so is that like the "Controller"? Though the .aspx file also has some functions that seem to not be entirely view-related...

Could someone give me a quick overview or a place to start with this?

tereško
  • 56,151
  • 24
  • 92
  • 147
Kalina
  • 5,244
  • 16
  • 59
  • 98
  • One takes HTML/ASP markup, the other takes C# code. Just looking at the auto-generated code should probably tell you that much. – Servy Nov 01 '12 at 17:51
  • A good place to start would be http://www.asp.net/. Good overview of the technology with tutorials. – System Down Nov 01 '12 at 17:53

5 Answers5

20

ASPX files usually will have the UI and will which is usually HTML tags, some ASP.NET server control embed code (which ultimately produce some HTML markups). ASPX.CS file (usually called the codebehind) will have server-side coding in C#.

If needed, I would relate ASPX page to View and ASPX.CS to Controller action methods.

You have to remember that in webforms, there are ASP.NET controls we will be using in the ASPX file to render some HTML. Examples are TextBox, DataGrid, etc. In MVC, there is nothing called Server control. The View will be pure, handwritten HTML.

If needed, you can create a Hybrid project which is a combination of MVC and webforms. Scott has a post explaining about it here.

No ViewState in MVC :)

When switching from ASP.NET Webforms to MVC, One important thing you have to understand is that MVC architecture tries to stick with the truth that HTTP is stateless. There is no viewstate available in MVC. So you need to make sure that you are repopulating the data in every HTTP Request, as needed. Folks usually run into problems in loading DropDownlist in MVC. There are a lot of answers here in SO about how to handle dropdown lists on postback (when form is posted).

I suggest that you look into some beginner-level tutorials on ASP.NET MVC and start building your app step-by-step, and if you run into any issues, post a (new) question with relevant details.

Good luck, and welcome to the wonderful world of MVC. :)

Shyju
  • 197,032
  • 96
  • 389
  • 477
2

It sounds like you haven't created an MVC project, but rather a WebForms project.

The *.aspx files are the markup and the *.aspx.cs files are the code-behind files. Code-behind files handle the .NET code for any server-side controls in the *.aspx files.

Justin Niessner
  • 229,755
  • 35
  • 391
  • 521
  • The .aspx and .aspx.cs file I'm referring to are from the non-MVC project that I was given for reference. – Kalina Nov 01 '12 at 17:53
2

Checkout Wikipedia's document on ASP.NET, http://en.wikipedia.org/wiki/ASP.NET.

It states:

Web forms are contained in files with a ".aspx" extension; these files typically contain static (X)HTML markup, as well as markup defining server-side Web Controls and User Controls where the developers place all the rc content for the Web page. Additionally, dynamic code which runs on the server can be placed in a page within a block <% -- dynamic code -- %>, which is similar to other Web development technologies such as PHP, JSP, and ASP. With ASP.NET Framework 2.0, Microsoft introduced a new code-behind model which allows static text to remain on the .aspx page, while dynamic code remains in an .aspx.vb or .aspx.cs or .aspx.fs file (depending on the programming language used).

The .cs file names .aspx.cs is the code behind that goes with .aspx, which generally holds the html, css, javascript and other client side controls.

Generally, dynamic code (C# in this case because of the .cs on the file name) goes in the .cs file as a "good practice" to keep dynamic code and static html separated. Another reason for this abstraction is that the .aspx.cs code is run server side, while the .aspx file is compiled on the server and is then served to the web client requesting it.

Additionally, for MVC, I would suggest using a different view model, specifically Razor, which uses .cshtml files instead of the .aspx.cs and .aspx because they are easier to follow. The reason for the change in MVC is that MVC uses the MVC pattern to abstract layers of code so that .aspx and .aspxcs are not as needed. From a personal experience, I have used both Razor and Webforms (.aspx/.aspx.cs) view models with MVC and I find Razor to be much easier to code/maintain and use.

Ben Hoffman
  • 7,769
  • 6
  • 41
  • 69
1

The aspx file contains your page markup. It's automatically converted into code by ASP.NET.

The cs file contains the code behind your page (initialization, event handlers, etc.). You have to write that code yourself.

These two files are related with the inheritance and he Inherits attribute of the @Page directive associates the page markup to the code behind

Kundan Singh Chouhan
  • 12,877
  • 4
  • 23
  • 30
0

.aspx is your markup file. Contains things such as HTML, CSS, JavaScript, and ASP markup. this .cs file is referred to as a codebehind file. This is where you do thing that may not be available or u are not comfortable doing in scripting languages. Generally aspx is run on the client side while the code behind is executed on the server.

Antarr Byrd
  • 20,852
  • 26
  • 85
  • 154