What is the difference between .aspx and .aspx.cs? - c#

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?

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 "code-behind") 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. :)

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.

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.

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

.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.

Related

What is happening with html file with asp #page directive and content tags within it

With ASP.net I am familiar with creating a master page and then being able to set that master page for a .aspx file.
With one of our sites where I work we are given ftp access to edit static html pages. Within these html pages are a few asp tags, specifically a page directive with title, language and masterpagefile attributes, and a few content tags with id, contentplaceholderid and runat attributes.
My question is how are they doing this? If I were to add a master page in visual studio for my own project I am unable to pick a master page for .html files. Are they somehow building/overwriting a .aspx file after upload? If I go to the website the url will show a .html extension.
Within these html pages are a few asp tags, specifically a page directive with title, language and masterpagefile attributes, and a few content tags with id, contentplaceholderid and runat attributes.
Then the host is processing these .html files through the ASP.NET runtime before serving them. At a technical level ASP.NET pages don't need to have the .aspx file extension, that's just by convention. The web server can be configured to process any files as ASP.NET pages.
If you look through the configuration of IIS for example (and the specifics of that will vary wildly with IIS versions), you might find something like (and this may be quite dated, I haven't had to look under the hood in IIS for years) a mapping for .aspx files to aspnet_isapi.dll. That same configuration can also be changed to also map .html files to aspnet_isapi.dll, which would process them the same way.
This isn't common practice not only because of convention but also because server-side processing adds overhead and is less performant than serving a static file directly from the web server. Since .html files traditionally have static content, it's a lot faster to let IIS serve them directly than to process them through the ASP.NET engine. It's possible, and the host in question seems to be doing exactly that, it's just uncommon for these reasons.
In Visual Studio you can change the settings to serve Dynamic content.
In Application Designer, you must specify whether an ASP.NET application exposes dynamic or static content. Dynamic content specifies that a script processor such as ASP.NET is required to run the application or any Web services associated with the application. Static content specifies that no script processor is required, for example, to run the application or to display HTML.
http://msdn.microsoft.com/en-us/library/ms242474(v=vs.90).aspx

Can I override views in Web Forms like I can in MVC?

In ASP.NET MVC I can typically override view e.g. by putting a view with the same name in the DisplayTemplates folder. If I wanted to override the way images are rendered, I could put something like Images.cshtml in the folder.
Now, I want to override the way xforms are rendered in EPiServer. I know how to do this in ASP.NET MVC, but this project uses Webforms.
I have tried to search, but the documentation seems sparse on the subject. In ASP.NET MVC, I could e.g. extend the search engine to search specific locations to look for my views, or put them where ASP.NET looks by default.
This doesn't seem to work in Web Forms. Does anyone know how?
EDIT: EPiServer has an .ascx file which it uses to render an XForm with. I want to tell ASP.NET to use my .ascx file instead. To do this I need to tell ASP.NET to look for my .ascx file, e.g. by telling the ASP.NET view engine to look for my .ascx in a specific folder, or by placing it somewhere the view engine looks by default.
How do I do this?
If you want to replace it everywhere just replace the ascx file. Otherwise I am afraid the answer is no. Web Forms does not look for alternative locations for files by default as controls are usually specified using the full path or the class name. What you want to do would be equivalent of C# looking for alternative namespaces when it cannot find a class name. There are ways to achieve this behavior in Web Forms like for example Dynamic Data but ascx controls is not this.
If you are using the Property web control to display the value you can create your own custom PropertyControl and register it for your type in the PropertyControlFactory. This way you can control how your property will be rendered.
While this doesn't allow you to point out your .ascx directly, you can load it in your server control if you prefer that.
For code examples and a great summary of this (and some other) ways of customizing property rendering in EPiServer, see Mathias Kunto's blog post at http://blog.mathiaskunto.com/2012/03/05/being-friends-with-the-propertycontrolclassfactory-or-101-ways-to-change-episerver-built-in-property-appearances/.

What to use: .aspx or .html

I am creating a website in .aspx, and while adding more pages I saw that I can choose for an .aspx-extension and for a .html-extension.
I am a bit confused at the moment. Normally I am using always the .aspx one, but what is the difference between them and which of them do you guys prefer me to use?
Thank you very much.
This is the biggest challenge for the beginners. Aspx pages and asp.net webservercontrols are like machines that spit out html. if you want a logic to spit html on the page according to that logic then use .aspx or Webservercontrols on that page. if you don't have very serious decisions to make programmatically and need straight forward html that does not change based on some events then use .html pages. But if you are on something like asp.net then it means you may have to make decisions based on data from the server so in that case .aspx would be requirement than an option.
An HTML file can only contain static content (it can not contain any server side code), it will not be processed by the ASP.NET pipeline.
So normally you should always use aspx files in an ASP.NET app, except if you have a page which only has to contain static HTML, that way you might save some performance, because IIS can directly serve that file without running it through the whole pipeline.
.htm/l files are generally used for static markup, but they can be .NET-enabled also (using configuration options). That is to say, other extensions can be compiled and run through the ASP.NET ISAPI handler to be pages that have code-behind and managed controls and "do stuff" server-side.
I guess it's preference, so you could decide to use .htm/l for server-handled pages and .aspx for handled pages. I personally don't find this option appealing and have to deal with, at least weekly, an .htm/l page that didn't need any .NET stuff but now does. It doesn't make sense to me to mix up the files like this - the effort for IIS should be negligible, particularly for .aspx pages that contain only static content. It's much less effort than having to wonder about changing file names (and therefore URLs or rewrites) or setting global configuration options to handle certain extensions because modifications were needed.

Can I have a folder in my ASP.NET MVC4 project that is a webforms project?

I tried googling, I promise! I may not be asking the question the right way. We have an existing project that is webforms (.NET 3.5 I think). It's not really a VS solution, just a folder with this structure:
/
../App_Code
../bin
../pages
../global.asax
../this.html
../that.aspx
../web.config
In the "pages" folder is where we have a big ugly mess of .aspx pages and there code behind.
App_Code holds some helper classes and whatnot. They rest should be self explanatory.
Questions:
What is the best strategy to put this mess inside an mvc4 application?
What do I have to do with routing back and forth (i.e. from .cshtml pages to .aspx and back again)
Any other considerations?
Yes, you can do it. I would recommend adding this to your routeConfig:
routes.IgnoreRoute("pages/{*pathInfo}");
It may not be even absolutely necessary, but it'll keep the request from even attempting to be parsed out in the routecollection. Just incase you have a page and a route rule that can collide. We do this for our webservices which reside in our MVC 4 application (inherited from an older website project).
That will just work.
You can mix and match any kind of ASP.Net stuff in one project.
The ASPX files will be accessible using their actual paths, just like pure WebForms project.
You can also call MapPageRoute() to apply routing to those files.

How to fix my ASP.NET Webforms structure?

I have a problem with the structure of an old asp.net webform page. Now i want to convert this into an MVC page, because i think there a several things which does MVC automatically...
Here are the facts about the software:
The software is not a "real" asp.net project, just a collaction of many aspx pages which are called with ajax (single pages for every event).
One page for "ModuleOne/save/"
One page for "ModuleOne/edit/"
One page for "ModuleOne/create/" and so on... I think you know what i want to tell you...
The software use two different languages (vb.net, c#) inside the app_code directory with two subdirectories (vbcode, csharpcode)
The software use the "yui compress plugin (js/css)", which is good but there are a lot of better plugins in mvc (do it automatically with bundles...)
The global.asax has a lot of routes to the aspx pages
Now, i want to fix theses problems with the following points:
Convert the collaction of asp.net pages to a "real" asp.net web app project
Convert the vb.net classes to one dll file and add a new reference on it
Eleminate the "yui compress plugin" and replace it with the functionality in mvc (bundles)
Eliminate the "manual routes" in the global.asax an replace this with the "controller handling" in mvc.
In my opinion mvc is the best solution to fix all theses "project-structure-problems"?!
What do you think about this solution/new structure? Make this sense?

Categories