Microsoft 9GD00001 ユーザーズマニュアル

ページ / 449
 
262
 
Microsoft Visual Studio 2010: A Beginner’s Guide
Managing Routing
ASP.NET MVC has a routing system that matches URLs to controllers with actions and 
the parameters passed to those actions. When you start a new ASP.NET MVC project, 
default routing will be established via a file called Global.asax, which is where many 
events affecting the application are placed. When you run an ASP.NET MVC application, 
it will use URLs of the form http://domain/controller/action/param1/param2/…/paramN? 
optionalArg=optionalVal. Here’s an example:
http://localhost:1042/Home/About
In this example, localhost:1042 is the domain, Home is the Controller, and About is the 
action. When ASP.NET MVC sees this URL, it will instantiate the HomeController class 
and call the About method.
The Global.asax file has an Application_Start event that is called the first time the 
application runs. This is where routing is set up so that it will be in place for all of the 
requests while the application is running. Listing 9-5 shows the default routing for an ASP 
.NET MVC project.
Listing 9-5 
  Setting up routing
C#:
using System; 
using System.Collections.Generic; 
using System.Linq; 
using System.Web; 
using System.Web.Mvc; 
using System.Web.Routing; 
 
namespace MyShopCS 

    // Note: For instructions on enabling IIS6 or IIS7 classic mode, 
    // visit http://go.microsoft.com/?LinkId=9394801 
 
    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 {