- Get Started Using ASP.NET MVC for Your .NET Applications
- WebConfig.xml / Global.asax
- Home Controller / Home View
WebConfig.xml
If you open the application's root web.xml file, you'll notice that several services are already configured for the project, including the Role, Authentication, and Profile services:
<configSections> [ … ] <sectionGroup name="webServices" type="System.Web.Configuration.ScriptingWebServicesSectionGroup, System.Web.Extensions, Version=3.5.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35"> <section name="profileService" type="System.Web.Configuration.ScriptingProfileServiceSection, System.Web.Extensions, Version=3.5.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35" requirePermission="false" allowDefinition="MachineToApplication" /> <section name="authenticationService" type="System.Web.Configuration.ScriptingAuthenticationServiceSection, System.Web.Extensions, Version=3.5.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35" requirePermission="false" allowDefinition="MachineToApplication" /> <section name="roleService" type="System.Web.Configuration.ScriptingRoleServiceSection, System.Web.Extensions, Version=3.5.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35" requirePermission="false" allowDefinition="MachineToApplication" /> </sectionGroup> [ … ] </configSections>
The file also includes a database connection to the embedded SQL Server that comes with Visual Studio (SQL Express). This is how our application is able to register users using the providers. Everything else in the file is fairly self-explanatory (provided you've worked with other types of .NET applications). One thing to note, however, would be the URL routing Module and URL routing Handler. This module/handler is essential for how AMVC maps requests to URLs.
Global.asax
The Global.asax file contains the mapping template(s) and rules for how requests will be mapped to URLs. AMVC sets up a default, and highly useful mapping rule. The code for this is as follows:
public class MvcApplication : System.Web.HttpApplication { public static void RegisterRoutes(RouteCollection routes) { routes.IgnoreRoute("{resource}.axd/{*pathInfo}"); routes.MapRoute( "Default", // Route name "{controller}/{action}/{id}", // URL with parameters new { controller = "Home", action = "Index", id = UrlParameter.Optional } // Parameter defaults ); }
As you can see, the routing template follows an easy convention of having the controller name, action, and/or id as the URL pattern. This makes it easy for developers to know what controllers and actions belong to what view in the Presentation level of the application, even without looking at any code. It is also good for users, because it makes good logical sense. Using the rule template above, a Home rule was created for us to map the Index action to the Home controller, which results in the URL path like http://localhost:3897/home/index/.