Implementing default routes in VS .NET

Encoder Code 128 in VS .NET Implementing default routes

Listing 16.2 Implementing default routes
Code 128 Code Set A Encoder In .NET Framework
Using Barcode maker for ASP.NET Control to generate, create Code 128B image in ASP.NET applications.
www.OnBarcode.com
Make QR Code In .NET
Using Barcode creator for ASP.NET Control to generate, create QR Code JIS X 0510 image in ASP.NET applications.
www.OnBarcode.com
public class MvcApplication : HttpApplication { public static void RegisterRoutes(RouteCollection routes) { routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
UCC - 12 Generation In .NET Framework
Using Barcode printer for ASP.NET Control to generate, create GTIN - 12 image in ASP.NET applications.
www.OnBarcode.com
1D Barcode Generator In .NET
Using Barcode creator for ASP.NET Control to generate, create 1D image in ASP.NET applications.
www.OnBarcode.com
Ignores route
Print Barcode In .NET Framework
Using Barcode creator for ASP.NET Control to generate, create Barcode image in ASP.NET applications.
www.OnBarcode.com
Printing ECC200 In Visual Studio .NET
Using Barcode creation for ASP.NET Control to generate, create Data Matrix image in ASP.NET applications.
www.OnBarcode.com
Implementing routes in ASP.NET MVC
Drawing EAN128 In VS .NET
Using Barcode generation for ASP.NET Control to generate, create EAN / UCC - 13 image in ASP.NET applications.
www.OnBarcode.com
ANSI/AIM I-2/5 Drawer In VS .NET
Using Barcode printer for ASP.NET Control to generate, create ITF image in ASP.NET applications.
www.OnBarcode.com
routes.MapRoute( "Default", "{controller}/{action}/{id}", new { controller = "Home", action = "Index", id = UrlParameter.Optional } ); } protected void Application_Start() { RegisterRoutes(RouteTable.Routes); } }
Recognize USS Code 128 In None
Using Barcode scanner for Software Control to read, scan read, scan image in Software applications.
www.OnBarcode.com
Printing Code-128 In VS .NET
Using Barcode generation for ASP.NET Control to generate, create Code128 image in ASP.NET applications.
www.OnBarcode.com
237 Defines route
Create Barcode In None
Using Barcode maker for Software Control to generate, create Barcode image in Software applications.
www.OnBarcode.com
UPC-A Generation In None
Using Barcode encoder for Microsoft Excel Control to generate, create UPC-A Supplement 2 image in Office Excel applications.
www.OnBarcode.com
In listing 16.2, the first operation is an IgnoreRoute B. We don t want Trace.axd, WebResource.axd, and other existing ASP.NET handlers routed through the MVC Framework, so the route {resource}.axd/{*pathInfo} ensures any request coming in with an extension of .axd won t be served by ASP.NET MVC. The second operation defines our first route. Routes are defined by calling MapRoute on a RouteCollection C, which adds a Route object to the collection. So, what comprises a route A route has a name, a URL pattern, default values, and constraints. The latter two are optional, but you ll most likely use default values in your routes. The route in listing 16.2 is named Default, has a URL pattern of {controller}/{action}/{id}, and includes a default value dictionary that identifies the default controller and action. These default values are specified in an anonymous type, which was introduced in .NET 3.5 and carries forward into .NET 4. If we pick apart this route, we can easily see its components: the first segment of the URL will be treated as the controller, the second segment as the action, and the third segment as the ID. Notice how these values are surrounded in curly braces. When a URL comes in with the following format, what do you think the values will be for controller, action, and ID
Code 128B Recognizer In Visual C#.NET
Using Barcode scanner for .NET Control to read, scan read, scan image in .NET framework applications.
www.OnBarcode.com
Generating QR Code 2d Barcode In Objective-C
Using Barcode maker for iPhone Control to generate, create QR Code image in iPhone applications.
www.OnBarcode.com
http://example.com/users/edit/5
PDF417 Recognizer In None
Using Barcode decoder for Software Control to read, scan read, scan image in Software applications.
www.OnBarcode.com
Code 3 Of 9 Creator In Objective-C
Using Barcode printer for iPad Control to generate, create Code-39 image in iPad applications.
www.OnBarcode.com
Figure 16.3 shows how the values are pulled out of the URL. Remember, controller id this is only the default route template. You re free to change this for your own applications. http://example.com/users/edit/5 The route values, shown in action table 16.2, are all strings. The controller will be extracted out of this Figure 16.3 Decomposing a URL into route values using URL as users. The controller part the default route of {controller}/{action}/{id} of the class name is implied by convention, so the controller class created will be UsersController. As you can probably already tell, routes aren t case sensitive. The action describes the name of the method to call on our controller. In ASP.NET MVC, an action is defined as a public method on a controller that returns an
Code 128 Printer In .NET Framework
Using Barcode generation for VS .NET Control to generate, create Code 128 Code Set B image in Visual Studio .NET applications.
www.OnBarcode.com
Read GS1 - 13 In Java
Using Barcode reader for Java Control to read, scan read, scan image in Java applications.
www.OnBarcode.com
Routing
Print GS1-128 In Objective-C
Using Barcode creation for iPad Control to generate, create GS1-128 image in iPad applications.
www.OnBarcode.com
GS1 - 12 Generation In Objective-C
Using Barcode encoder for iPad Control to generate, create UPC-A Supplement 2 image in iPad applications.
www.OnBarcode.com
Name Controller Action ID
Value
"users" "edit" "5"
Table 16.2 The route values, set to the values extracted from the URL
ActionResult. By convention, the framework will attempt to find a method on the
specified controller that matches the name supplied for the action. If none is found, it will also look for a method that has the ActionNameAttribute applied with the specified action. The remaining values defined in a route are pumped into the action method as parameters, or left in the Request.Params collection if no method parameters match. Notice that the ID is also a string, but if your action parameter is defined as an integer, a conversion will be done for you. Listing 16.3 shows the action method that will be invoked as a result of the URL in figure 16.3.
Listing 16.3 An action method matching http://example.com/users/edit/5
public class UsersController : Controller { public ActionResult Edit(int id) { return View(); } }
What happens if we omit the ID or action from our URL What will the URL http:// example.com/users match To understand this, we have to look at the route defaults. In our basic route defined in listing 16.2, we can see that our defaults are defined as
new { controller = "Home", action = "Index", id = UrlParameter.IOptional }
This allows the value of "Index" to be assumed when the value for action is omitted in a request that matches this route. You can assign a default value for any parameter in your route. We can see that the default routes are designed to give a reasonable level of functionality for an average application, but in almost any real-world application you want to design and customize a new URL schema. In the next section, we ll design a URL schema using custom static and dynamic routes.
Copyright © OnBarcode.com . All rights reserved.