Managing links and URLs with T4MVC in VS .NET

Creation ANSI/AIM Code 128 in VS .NET Managing links and URLs with T4MVC

21.2 Managing links and URLs with T4MVC
Making Code 128A In .NET
Using Barcode maker for ASP.NET Control to generate, create Code 128 Code Set C image in ASP.NET applications.
www.OnBarcode.com
Draw Barcode In .NET Framework
Using Barcode generator for ASP.NET Control to generate, create Barcode image in ASP.NET applications.
www.OnBarcode.com
Out of the box, ASP.NET MVC contains many opportunities to get tripped up with magic strings, especially with URL generation. Magic strings are string constants that
Code 3 Of 9 Creator In VS .NET
Using Barcode printer for ASP.NET Control to generate, create Code-39 image in ASP.NET applications.
www.OnBarcode.com
Painting PDF-417 2d Barcode In Visual Studio .NET
Using Barcode generator for ASP.NET Control to generate, create PDF 417 image in ASP.NET applications.
www.OnBarcode.com
Organization with areas
Making QR In Visual Studio .NET
Using Barcode generation for ASP.NET Control to generate, create QR Code JIS X 0510 image in ASP.NET applications.
www.OnBarcode.com
Linear Encoder In VS .NET
Using Barcode creation for ASP.NET Control to generate, create 1D image in ASP.NET applications.
www.OnBarcode.com
are used to represent other constructs, but with an added disconnect that can lead to subtle errors that only show up at runtime. To provide some intelligence around referencing controllers, views, and actions, the T4MVC project helps by generating a hierarchical code model representation for use inside controllers and views. In listing 21.8, our Edit action contains a BeginForm method call that references the Save action on the Profile controller, using magic strings to build the URL for the form element.
Drawing Code 128 Code Set B In VS .NET
Using Barcode encoder for ASP.NET Control to generate, create Code 128A image in ASP.NET applications.
www.OnBarcode.com
Encoding C 2 Of 5 In .NET Framework
Using Barcode drawer for ASP.NET Control to generate, create 2 of 5 Industrial image in ASP.NET applications.
www.OnBarcode.com
Listing 21.8 A brittle Edit view with magic strings
Code 128 Code Set A Encoder In Java
Using Barcode printer for Java Control to generate, create Code-128 image in Java applications.
www.OnBarcode.com
Generate USS Code 128 In None
Using Barcode creator for Microsoft Word Control to generate, create Code-128 image in Office Word applications.
www.OnBarcode.com
<% using (Html.BeginForm("Save", "Profile")) {%> <%= Html.EditorForModel() %> <p> <input type="submit" value="Save" name="SaveButton" /> </p> <% } %>
QR Code JIS X 0510 Reader In C#.NET
Using Barcode scanner for .NET framework Control to read, scan read, scan image in .NET framework applications.
www.OnBarcode.com
Reading Barcode In .NET Framework
Using Barcode decoder for VS .NET Control to read, scan read, scan image in VS .NET applications.
www.OnBarcode.com
The magic strings in listing 21.8 lie in the Html.BeginForm method. The strings "Save" and "Profile" are route data that refer to a ProfileController class and Save method. If we were to change the name of our controller and action via built-in refactoring tools, our Edit view would then break. Ideally, all the places where we reference controllers, actions, views, and route values by magic strings could be replaced by something more resilient to the inevitable changes we see in most projects. In the previous section, we saw hard-coded route data values reference "area". If we were to accidentally mistype or misspell the area route entry or value, our application would break at runtime. To eliminate these potential problems, we have two options. We can use constants and strongly typed, expression-based URL generation, or we can use a form of code generation that allows us to easily reference views, controllers, and actions. The T4MVC project, which is part of MvcContrib (http://mvccontrib.org), uses T4 (Text Template Transformation Toolkit) templates to generate extension methods, view name constants, and action link helpers to eliminate the pesky magic strings that would otherwise litter our application. The T4MVC templates use the T4 templating technology introduced with Visual Studio 2008. To use T4MVC, we first need to download the latest T4MVC release from http://mvccontrib.codeplex.com/ wikipage title=T4MVC and place the following two files in the root of our application:
UPC Symbol Maker In Objective-C
Using Barcode creation for iPhone Control to generate, create UPCA image in iPhone applications.
www.OnBarcode.com
2D Barcode Printer In C#
Using Barcode generator for .NET framework Control to generate, create 2D Barcode image in .NET applications.
www.OnBarcode.com
T4MVC.tt T4MVC.settings.t4
Creating Barcode In None
Using Barcode drawer for Software Control to generate, create Barcode image in Software applications.
www.OnBarcode.com
Recognize Barcode In Java
Using Barcode Control SDK for BIRT Control to generate, create, read, scan barcode image in Eclipse BIRT applications.
www.OnBarcode.com
Figure 12.7 Our application, including the two T4MVC template files
EAN128 Drawer In Java
Using Barcode printer for Java Control to generate, create EAN128 image in Java applications.
www.OnBarcode.com
Print Data Matrix 2d Barcode In Objective-C
Using Barcode encoder for iPad Control to generate, create DataMatrix image in iPad applications.
www.OnBarcode.com
In figure 21.7, we see these two files added to the root of our MVC application.
1D Barcode Printer In Java
Using Barcode creator for Java Control to generate, create Linear 1D Barcode image in Java applications.
www.OnBarcode.com
Making PDF-417 2d Barcode In Java
Using Barcode maker for Java Control to generate, create PDF417 image in Java applications.
www.OnBarcode.com
Managing links and URLs with T4MVC
The T4 template security dialog
When the T4MVC templates are added to the project, or when the project is built or run, the templates are regenerated. In some environments, a security dialog box may pop up, as shown in figure 21.8. You can check the Do Not Show This Message Again check box if you don t want this dialog box showing up again, and click the OK button to run the template generation. The T4MVC template modifies existing controllers, making them partial classes, and generates a set of helper files. These helper files, shown in figure 21.9, include a set of codegenerated controller partial classes Figure 21.9 Helper files generated from the T4MVC and extension methods. templates With partial classes, the T4MVC templates generate a set of helper methods and properties that allow us to easily refer to controllers, actions, and views from anywhere in our application. For example, the original LogOff action in the AccountController was rife with magic strings, as shown in listing 21.9.
Listing 21.9 The original LogOff action
public virtual ActionResult LogOff() { FormsService.SignOut(); return RedirectToAction("Index", "Home"); }
Instead of referring to the Index action on the Home controller by strings, we can instead navigate the hierarchy created in the generated MVC class shown in listing 21.10.
Organization with areas
Listing 21.10 Using the generated MVC class to refer to controllers and actions
public virtual ActionResult LogOff() { FormsService.SignOut(); return RedirectToAction(MVC.Home.Index()); }
Internally, the new RedirectToAction method lives on the generated partial controller class. The Index method in listing 21.10 records the controller and action name, allowing the generated RedirectToAction method to build the correct ActionResult. All of this is behind the scenes, and our existing controllers can start using the new generated overloads to generate ActionResult objects. In our views, we ll use some generated HtmlHelper extension methods for generating action links and URLs. Listing 21.11 shows our modified logon partial.
Copyright © OnBarcode.com . All rights reserved.