- Home
- Products
- Integration
- Tutorial
- Barcode FAQ
- Purchase
- Company
vb.net code 128 font Extending the routing system in Visual Studio .NET
24.1 Extending the routing system Make USS Code 128 In .NET Framework Using Barcode creation for ASP.NET Control to generate, create Code 128 Code Set B image in ASP.NET applications. www.OnBarcode.comLinear 1D Barcode Creator In .NET Framework Using Barcode maker for ASP.NET Control to generate, create Linear Barcode image in ASP.NET applications. www.OnBarcode.comThe UrlRoutingModule is an implementation of IHttpModule and represents the entry point into the ASP.NET MVC Framework. This module examines each request, builds up the RouteData for the request, finds an appropriate IRouteHandler for the given route matched, and finally redirects the request to the IRouteHandler s IHttpHandler. In any ASP.NET MVC application, the default route looks like the one in listing 24.1. The MapRoute method is a simplified way of specifying routes. UPC-A Supplement 2 Printer In Visual Studio .NET Using Barcode generator for ASP.NET Control to generate, create GTIN - 12 image in ASP.NET applications. www.OnBarcode.comEAN128 Generator In Visual Studio .NET Using Barcode printer for ASP.NET Control to generate, create GS1 128 image in ASP.NET applications. www.OnBarcode.comDebugging routes
Code 3 Of 9 Maker In .NET Framework Using Barcode maker for ASP.NET Control to generate, create Code 39 Full ASCII image in ASP.NET applications. www.OnBarcode.comPrint Barcode In .NET Framework Using Barcode printer for ASP.NET Control to generate, create Barcode image in ASP.NET applications. www.OnBarcode.comListing 24.1 MapRoute, used to specify routes
Barcode Creation In VS .NET Using Barcode creation for ASP.NET Control to generate, create Barcode image in ASP.NET applications. www.OnBarcode.comEncode European Article Number 8 In Visual Studio .NET Using Barcode encoder for ASP.NET Control to generate, create EAN8 image in ASP.NET applications. www.OnBarcode.comroutes.MapRoute("default", "{controller}/{action}/{id}", new { Controller="home", Action="index", id=UrlParameter.Optional}); Code 128 Generator In Visual C#.NET Using Barcode maker for .NET Control to generate, create Code 128C image in Visual Studio .NET applications. www.OnBarcode.comDraw Code 128B In Java Using Barcode generation for Java Control to generate, create Code-128 image in Java applications. www.OnBarcode.comMost of the applications you ll work with will use this style of adding routes. There s also a more verbose method, which allows us to customize the classes that are used as part of the route. Listing 24.2 shows the same route but without using the MapRoute helper method. QR Code ISO/IEC18004 Maker In Java Using Barcode maker for Java Control to generate, create QR Code ISO/IEC18004 image in Java applications. www.OnBarcode.comGTIN - 13 Creation In Objective-C Using Barcode drawer for iPad Control to generate, create GS1 - 13 image in iPad applications. www.OnBarcode.comListing 24.2 A more detailed way of specifying routes
Drawing QR Code JIS X 0510 In .NET Framework Using Barcode drawer for VS .NET Control to generate, create Quick Response Code image in .NET framework applications. www.OnBarcode.comDecode Data Matrix ECC200 In None Using Barcode decoder for Software Control to read, scan read, scan image in Software applications. www.OnBarcode.comroutes.Add(new Route("{controller}/{action}/{id}", new RouteValueDictionary(new { Controller = "home", Action = "index", Specifies route id = UrlParameter.Optional }), handler new MvcRouteHandler() )); Decode Code 39 Extended In .NET Framework Using Barcode recognizer for VS .NET Control to read, scan read, scan image in VS .NET applications. www.OnBarcode.comPDF-417 2d Barcode Recognizer In Java Using Barcode decoder for Java Control to read, scan read, scan image in Java applications. www.OnBarcode.comThat third argument in listing 24.2 B tells the framework which IRouteHandler to use for this route. We re using the built-in MvcRouteHandler that ships with the framework. This class is used by default when we call the MapRoute method, but we can change this to a custom route handler and take control in interesting ways. An IRouteHandler is responsible for creating an appropriate IHttpHandler to handle the request, given the details of the request. This is a good place to change the way routing works, or perhaps to gain control extremely early in the request pipeline. The MvcRouteHandler simply constructs an MvcHandler to handle a request, passing it a RequestContext, which contains the RouteData and an HttpContextBase. A quick example will help illustrate the need for a custom route handler. When defining our routes, we ll sometimes run across errors. Let s assume we ve defined the route shown in listing 24.3. Draw QR Code 2d Barcode In Visual Basic .NET Using Barcode drawer for .NET Control to generate, create QR Code image in Visual Studio .NET applications. www.OnBarcode.comRecognizing PDF417 In Visual Basic .NET Using Barcode recognizer for .NET Control to read, scan read, scan image in Visual Studio .NET applications. www.OnBarcode.comListing 24.3 Adding another route
PDF-417 2d Barcode Encoder In None Using Barcode printer for Word Control to generate, create PDF 417 image in Office Word applications. www.OnBarcode.comBarcode Maker In Objective-C Using Barcode maker for iPhone Control to generate, create Barcode image in iPhone applications. www.OnBarcode.comroutes.MapRoute("CategoryRoute", "{category}/{action}", new { Controller = "Products", Action="index" }); Here we ve added a new custom route at the top position that will accept URLs like /apparel/index, use the ProductsController, and call the Index action on it, passing in the category as a parameter to the action, as shown in listing 24.4. Listing 24.4 is a good example of a custom route that makes our URLs more readable. Listing 24.4 A controller action that handles the new route
public class ProductsController : Controller { public ActionResult Index(string category) { return View(); } } Extending the routing system
Now, let s assume that we have another controller, HomeController, which has an Index action to show the start page, as shown in listing 24.5. Listing 24.5 A controller action to respond to the default route
public class HomeController : Controller { public ActionResult Index() { return View(); } } We d like the URL for the action in listing 24.4 to look like /home/index; but if we try this URL, we ll get a 404 error, as shown in figure 24.1. Why The problem isn t apparent from that error message. We certainly have a controller called HomeController, and it has an action method called Index. If we dig deep into the routes, we can deduce that this URL was picked up by the first route, {category}/{action}, which wasn t what we intended. We should be able to quickly identify a routing mismatch so that we can fix it speedily. Figure 24.1 This message doesn t tell us much about what s wrong. An action couldn t be found on the controller, but which one Debugging routes
With many custom routes, it s easy for a URL to be caught by the wrong route. It d be nice if we had a diagnostic tool to display which routes are being matched (and used) so we could quickly catch these types of errors. 24.2 Inspecting routes at runtime
To see the route rules as they re matched at runtime, we can add a special query string parameter that we can tack onto the end of the URL. This will signify that instead of rendering the regular view, our custom route debugger should instead circumvent the request and provide a simple HTML view of the route information. The current route information is stored in an object called RouteData, available to us in the IRouteHandler interface. The route handler is also the first to get control of the request, so it s a great place to intercept and alter the behavior for any route, as shown in listing 24.6.
|
|