1

When you go to the root of an ASP website (either webforms or MVC), how does the web server know which .aspx or .cshtml file to load, and which dll in the bin/ directory contains the code to execute? How does it match the two up?

thecoop
  • 45,220
  • 19
  • 132
  • 189
  • For MVC, http://msdn.microsoft.com/en-us/library/system.web.routing.routetable.aspx might give you some information you're after – Chris S Jan 08 '13 at 14:10
  • How does it execute the code to create the routes in the first place, from the text-only Global.asax file that has no assembly references? – thecoop Jan 08 '13 at 14:23
  • Why did someone downvote this question? – Anonymous Jan 08 '13 at 14:43

1 Answers1

5

I don't know if your looking for a very technical answer, or a simple overview. So, here's a simple overview.

Default documents

When you go to the root of an ASP website

ASP, HTML, PHP or whatever technology you use, the Web server knows the default documents to search and show when no document was provided, for example, in Microsoft IIS Server, the default documents are:

enter image description here

This is the same for Apache server, there's a setting that tells the server "If you do not have a document name, use this one" setting, this one is found in .htaccess file and has:

DirectoryIndex  index.php index.html index.htm default.html default.htm home.html

In ASP.NET MVC, you work with Routing tables and it's in the Global.asax file that the routes are mentioned, either by specifying the routes directly or call an external file (class), and the common route is:

routes.MapRoute(
    "Default", // Route name
    "{controller}/{action}/{id}", // URL with parameters
    new { controller = "Home", action = "Index", id = UrlParameter.Optional } // Parameter defaults
);

Where this states that, if no route is provided, it will be the same as /Home/Index.

Assemblies

Regarding the DLLs part, it's all about your first document...

.NET Sites are pre-compiled and exposed their namespaces and properties, you will have in a ASP.NET Webforms, something like this in the first line of code:

<%@ Page Title="Home Page" Language="C#" 
    MasterPageFile="~/Site.Master" 
    AutoEventWireup="true" 
    CodeBehind="Default.aspx.cs" 
    Inherits="WebApplication3._Default" %>

This tells the server to run the WebApplication3._Default inside Default.aspx.cs and from there, it will attach any needed assemblies to run the code.

Community
  • 1
  • 1
balexandre
  • 73,608
  • 45
  • 233
  • 342
  • +1 Also, the bridge between the url entered by the user and how it is processed on the server is the Handler Mappings. For .aspx and .cshtml (or any extension), an HttpModule does the heavy lifting of finding the appropriate .dll or file to execute and serve to the browser. – mellamokb Jan 08 '13 at 14:06
  • @mellamokb I don't know if he wanted the technical overview or not, so I simply stated the normal part :) fell free to answer it with more technical details if you think he wants that, I'll upvote if correct :) – balexandre Jan 08 '13 at 14:11
  • Very informative! So how does it get from Default.aspx.cs to the assembly containing the compiled result of Default.aspx.cs? – thecoop Jan 08 '13 at 14:19
  • as @mellamokb said, it's all about **Handlers** (in IIS at least - don't know Apache as I'm a .NET developer), see [their importance **here**](http://www.iis.net/configreference/system.webserver/handlers), and, while in the subject check this [**better explanation**](http://msdn.microsoft.com/en-us/library/ms972974.aspx) on what's going on from the first request to the response in a IIS server – balexandre Jan 08 '13 at 14:29
  • Those don't seem to contain any information on how the aspnet_isapi.dll module locates the correct dll... :( – thecoop Jan 08 '13 at 14:39
  • @thecoop: Are you just curious, or trying to find an answer to some problem in your application? – mellamokb Jan 08 '13 at 14:43
  • As part of a program I'm writing, I need to reliably get from the directory containing a webforms or mvc webapp to the dll containing the code for that webapp in the `bin/` directory – thecoop Jan 08 '13 at 14:49