How asp.net application works? - asp.net

I am quite new to .NET development and I am just wondering how does it work?
My undermentioned points are:
While developing ASP.NET application, under the project we have files like:
pagename.aspx
pagename.aspx.cs
pagename.asp.desiger.cs
After adding certain functionality to pagename.aspx page, assuming I have the development required web application (this is not my concern, what is developed)
Now I'm going to deploy this application, I use web deployment MSI which creates the required files in the one folder called folderdelopyed.
This folder contains the files required to support this application but interesting does not contain pagename.aspx.cs and pagename.aspx.designer.cs files.
My question is if folderdelopyed does not contain .cs file, then how does it work to run the segment of code which I have written in this file called PageName.aspx.cs?

The code in your cs files gets compiled into a dll.
For Web Application projects this is one dll
For Web Site projects, this is a dll per page.
All of the code is now in the dll's in the bin folder of the website.
You can use a tool like ILSpy (http://wiki.sharpdevelop.net/ILSpy.ashx) to look inside the dll's and see your code.
In the old days, for classic ASP, the script used to be embedded in your page - a mix of code and HTML, and was interpreted at runtime.
I like the new way more :-)

ASP.NET code is compiled into Dynamic-link library files, also known as DLL files.
The code you write in your code behind, which is the files with .cs extension, is compiled and put into whole new file, with .dll extension - and that file is copied to the server, to the BIN folder of your site.
Depending on what project type you choose, it's possible to have several DLL files for the web application, changing in every build - see dash's answer for more details.
On every .aspx page you have referece to what DLL file to use, as the very first line. For example:
<%# Page Language="C#" AutoEventWireup="true" CodeBehind="pagename.aspx.cs" Inherits="MyNameSpace.pagename" %>
In this example, the Inherits part determines what DLL to use. How? By the namespace, which is also the name of the DLL file.
When the above .aspx is requested by a browser, the .NET engine will go to the BIN folder, look for MyNameSpace.dll and in there look for class called pagename that inherits from the base Page class - all the rest is typical life cycle of ASP.NET page.

let me to say you something more Amazing.
you can hide your aspx file too.and put their content in to dll as same as your cs file put in dll.
you can make k aspx that just contain an address to the ddl file and no html body :D
that was greate!!! not only you can hide your cs file, you can hide you aspx file too :D

Related

how to edit a file in an online aspx site?

I'm very new to asp.net ( but an aged PHP developer) ..
I'm now in a situation where I should do some small modification in an online aspx script(using vb.net).
I downloaded the whole site and opened it in visual studio, all the modification I had to do is to make a redirect to another external page after succesful login,I determined the place where I should do my modification inside a file named login.aspx.vb , and added this line in it:
Response.Redirect("My URL Here")
And then uploaded only the modified file again, to get no change at all.
I even tried to modify the success msg that appear after successful login and re-uploaded it to find no change at all ( still showing the old message)
Is there some step I'm missing before uploading the page?
You will need to compile the project, and if using visual studios you can just hit ctrl shift B and it will build the entire solution. Visual studio detects which project in the solution has changed and will compile it. Then do as the other answerer says and upload the .dll file which is a container of your compiled code that will be referenced by the host you upload it to when there is a request for it. (A container is a definite over simplification for more details check:
What is a dll?
In ASP.NET there are two types of projects.
Web application project
Web site project
In the first case, when you edit a .vb file you need to compile it with Visual Studio and upload the generated .dll file created in the bin folder.
In the second case you can edit the .vb files and upload them and IIS will compile them.
Based on what you wrote, you are in the first case. In that case you need to have the project / solution files to make any modifications in the .vb files. If you don't have access to those files you could inject some code in the .aspx or .ascx files.
For example:
<%
Dim flag As Boolean = false
// Write some code to set the flag
If (flag) Then
Response.Redirect("~/default.aspx")
End If
%>
Using <%....%> you can write code to execute when the page starts to render. This is a bad practice since because it has poor performance since the code is parsed and executed at runtime, while the code in .cs files is already compiled.
But if you don't have the project files you could do minor changes with this hacky approach.
Edit: If you are in the first scenario, you need to build the project and then upload its .dll file which is in the /bin folder. For any changes in the code files you upload only the .dll. If you change the .aspx, .ascx, etc files you need to upload those as well

Is server-side code in 'Single file aspx' compiled into a single dll in Web Application type of project?

If I have pages having server-side code in code-behind, then I know that the code-behind for all such pages will get compiled into a single intermediate language dll, when using a Web Application type of project.
But, if I have some single file aspx pages (i.e. pages with inline server-side code) in the same Web Application project, I am thinking that the server-side code for these inline aspx's will not be part of the single dll that is generated for a Web application project. Is this true?
My impression is that single-file aspx pages are always compiled on-the-fly and never pre-compiled.
Yes. It will be compiled on fly if it is inline in the ASPX file itself. But this will happen only once. After that it will be cached until the ASPX file is changed. So from the second request onwards both will be treated same.
You can read this
It is advisable to use single file when you expect the code to be changed frequently and you do not need to recompile the entire code.
Also here is the MSDN link for more details.
Note: You can have a single DLL with all code or you can have different DLLs for each page.

two problems with Web Site type project (not Web Application)

I have a project which is a Web Site, not Web Application and have two problems because of that.
I have a custom control derived from BaseValidator which I had to put in the App_Code folder. Now on the page I want to register this control by
<%# Register tagname="mytagname" Namespace="PP" TagPrefix="dv2" %>
But when I get enter to this page, I have exception, because the src attribute is missing. I can't put in the src attr. path to the App_Code because I have another exception. How can I do that? This is a Web Site, so the sources are not compiled to one dll file, so I don't know what to add to the src attribute.
When I want to add to the Web Site a new project library, I have to compile it and manually copy the library dll file to the bin folder in the Web Site. But don't know, how can I debug this library with brakepoints?
This is my first time with Web Site project type. I always created the Web App.
1- Don't use tagname attribute here, tagname is suitable for UserControls.
http://msdn.microsoft.com/en-us/library/c76dd5k1.aspx
2- There is no any difference between using library in a web app or website. Just choose Add Reference from project node's context menu and select your class library from project tab.
Try creating a separate project for your CustomControls and reference it in your WebSite project by the namespace you use in your CutomControls project. Also check out this post for some information asp.net add custom control in website
Your second question, if you keep all of your projects in the same solution and just reference them to each other's projects, you will be able to debug and step into methods and set break points in any of the projects in your solution. If you don't want to do that, you have to copy the debug symbol files along with the .dll to the bin or point VS to where the necessary debug symbol files are located and then you will be able to step through the code in the .dll

Does code in aspx page get compiled in a web application?

Let me just start by saying, if anyone knows of a good article that talks about this subject, please point me towards it.
Does code in an .aspx page (in between <% %> tags) get compiled in a web application or is it treated like markup where you can just change it without recompiling the solution? Does compiling only compile the code behind code in the .cs and designer.cs files ?
If you're developing in a 'Web Application Project' (as opposed to 'Web site') then any change to class (.cs), code-behind file (.aspx.cs), designer files, etc (basically anything that isn't markup or static files like .htm) would require a rebuild in Visual Studio.
All code within a .aspx file, in or out of code blocks (<%%>) gets compiled.
Changing an .aspx file will cause IIS to recompile it on the next request.
See MSDN - ASP.NET Dynamic Compilation.
Update (following comment):
As for development work - pretty much the same happens when you use the dev web server to view the page. You do not have to recompile the solution/project, but the page will get recompiled dynamically.

Page class outside of App_Code will not compile

I have a website that has 2 files as follows:
page.aspx
page.aspx.cs
It used to be that I could just drop new files onto the web server and IIS would automatically compile the files and I could access the page e.g.
http://www.website.com/page.aspx
... and the associated functionality in the page class contained in the .cs file would work nicely.
Now I get the error: "Could not load type namespace.classname" which refers to my page class.
Now for some strange reason I have to put all my .cs files, even page classes into the app_code folder.
All that has changed on my website is that I reorganised the structure so that instead of my pages being on the web root they are now inside http://.../newfolder/page.aspx.
For some reason all my page.aspx.cs files now have to be in app_code.
Any ideas?
Sounds like you are mixing up a Web Application Project and a Web Site.
Are you sure the files are exactly the same? Perhaps one #Page directive says CodeBehind=Page.aspx.cs and the other says CodeFile=Page.aspx.cs?
CodeBehind requires project compilation, so you cannot just drop in a new .cs file, you need to upload a new compiled DLL. CodeFile will allow dynamic compilation.
The App_Code directory is dynamically compiled (in both cases) when your app is accessed, so the Inherit directive has a valid type when you put the file there. In general, don't do this. You want the .cs file to go with the .aspx file. Use App_Code for business logic or utility classes that aren't associated with a particular page.
Finally, is this new subdirectory set up as a new app in IIS? What does the web.config file in your new directory change? Are you running the same version of ASP.NET? Check the "compilation" tag. I'm not sure what you could do there to cause this, but I'm sure you could cause some chaos.

Resources