Using URL Redirection ASP Help

I am going start with a simple technique to demonstrate how you can control request handling to achieve tasks that would otherwise be handled deep within the MVC framework. If you have been using the MVC framework for a while, you will know that you can redirect requests to alternate URLs by returning the result from methods such as Redirect and Redirect Permanent.

The MVC framework provides a set of redirection methods that can target a literal URL, a URL route, or another action method, and these redirections can be combined with other logic such that redirections happen only under certain circumstances (such as when requests contain particular form data values or originate from mobile devices). However, in mature projects, the most common reason for adding new redirections to action methods is to hide changes in the application structure. An example that I have seen a lot lately comes when a project switches from performing authentication locally to relying on a third-party such as Microsoft, Google, or Facebook. This could be addressed by changes in the routing configuration, but often the routes become so complex that performing the redirection in the controller is seen as the safer option. The action methods that would usually have received the authentication requests are replaced with redirections to new URL that can initiate the third-party authentication process.

Tip I demonstrate how to authenticate users through third parties in Part 3.

In this section, I am going to explain how redirections normally work in the MVC framework and how you can optimize this process by disrupting request execution and perform the redirection in a module. Table 6-2 puts module redirection in context.

Table 6-2. Putting Module Redirection in Context

Understanding the Normal Redirection Process

I have added a new action method to the Home controller that contains a redirection, as shown in Listing 6-3.

Listing 6-3. Adding an Action Method to the HomeController.cs File

The action method I added is called Authenticate, and it represents the scenario that I described: an action method whose original implementation has been replaced with a redirection. In this example, I perform the redirection by returning the result of the Redirect To Action method, which allows me to specify the names of the action method and the controller that the client will be redirected to.

You can see the effect of targeting the Authenticate action method by starting the application and requesting the /Home/Authenticate URL in the browser. You can see the sequence of requests to the application and the responses that are returned using the browser F12 developer tools (so called because they are accessed by pressing the F12 key), as illustrated by Figure 6-5.

Figure 6-5. Tracing the requests and responses in a redirection

Tip To see all the requests and responses, you must disable the Clear Entries on Navigate option at the top of the developer tools window.

The sequence of requests and responses is exactly like you would expect. The browser asks tile server for /Home/Authenticate but receives a 302 response, which tells tile browser to request a different URL. I specified tile Index action method in the Homecontroller, which corresponds to the default URL (I) in the default routing configuration, and that’s what tile browser is redirected to. The browser makes tile second request, and tile server sends tile content generated by the Index action method, which includes link elements for Bootstrap CSS files. The sequence is completed when the browser requests and receives the content of CSS files.

If tile only tiling that an action method is doing is issuing a redirection, then it becomes an expensive operation something that is easy to forget about, especially when refactoring a mature application where tile focus is on tile new functionality. The MVC framework is flexible and configurable, and it has to do a lot of work to get to the point where tile Redirect To Action method in Listing 6-3 is invoked and tile result is evaluated. The list includes tile following:

At every stage, tile MVC framework has to figure out which implementation is required and usually has to create some new objects, all of which takes time and memory. The RedirectToAction method creates a new RedirectToRouteResult object, which is evaluated and dues the work of performing tile redirection.

Simplifying the Redirection Process

The result of all tile work that the MVC framework has to go through to produce tile RedirectToRouteResult object is one of tile methods described in Table 6-3 being invoked on tile HttpResponse context object associated with tile request.

I can avoid having the MVC framework do all of that work by instead performing the redirection in a module. To demonstrate how this works, I created a folder called Infrastructure and added to it a class file called RedirectModule. cs.You can see the contents of the class file in Listing 6-4.

Listing 6-4. The Contents of the RedirectModule.cs File

Tip You might be wondering how I know that the UrlRoutingModule processes the request before the MapRequestHandler event. In fact, I looked at the source code for the module class and found that the request is processed in response to the PostResolveRequestCache event, which proceeds MapRequestHandler in the life cycle I described in Chapter 3. You can get the source code for the .NET Framework, including the ASP.NET platform from www.asphelponline.com This is separate from the source code for the MVC framework and the Web API, Be sure to read the licenses carefully because there are restrictions on how the source code can be used, especially for the .NET Framework cod

The RequestContext object provides information about the URL route that has matched the request and is accessed through the HttpRequest.RequestContext property. The RequestContext class defines the properties described in Table 6-4.

Table 6-4. The Properties Defined by the RequestContext Class

It is the RouteData object that gives me access to the information that I need, and I have described the three useful properties that RouteData defines in Table 6-5.

Table 6-5. The Properties defined by the RouteData Class

In my module, I use the RouteValueDictionary to determine the controller and action route values, which are used by the MVC framework to identify the controller and action method that the request will target if the values match the Authenticate action on the Homecontroller, then I perform a redirection, like this:

I could have specified the target URL for the redirection as a literal string value, but that would mean my module would have to be updated every time the routing configuration for the application changed, which is just the kind of thing that leads to brittle applications in the first place. Instead, I have used the UrlHelper class from the System. Web.Mvc namespace to generate a URL based on the name of the action method and controller that I want to target, as follows:

Once I have generated the URL from the routing configuration, I call the HttpResponse.Redirect method to send the response to the client and terminate any further request handling. Listing 6-5 shows how I registered the module in the Web.config file using the same approach.

Listing 6-5. Registering the Module in the Web.config File

To test the module, start the application and request the /Home/Authenticate URL. You can set a debugger breakpoint on the Authenticate action method in the controller class to prove that the method isn’t invoked when a request is redirected.