ASP.NET MVC Framework (Part 2): URL Routing
page 6 of 8
by Scott Guthrie
Feedback
Average Rating: This article has not yet been rated.
Views (Total / Last 10 Days): 43106/ 143

Unit Testing Routes

One of the core design principles of the ASP.NET MVC Framework is enabling great testing support.  Like the rest of the MVC framework, you can easily unit test routes and route matching rules.  The MVC Routing system can be instantiated and run independent of ASP.NET - which means you can load and unit test route patterns within any unit test library (no need to start up a web-server) and using any unit test framework (NUnit, MBUnit, MSTest, etc).

Although you can unit test an ASP.NET MVC Application's global RouteTable mapping collection directly within your unit tests, in general it is usually a bad idea to have unit tests ever change or rely on global state.  A better pattern that you can use is to structure your route registration logic into a RegisterRoutes() helper method like below that works against a RouteCollection that is passed in as an argument (note: we will probably make this the default VS template pattern with the next preview update):

Figure 25

You can then write unit tests that create their own RouteCollection instance and call the Application's RegisterRoutes() helper to register the application's route rules within it.  You can then simulate requests to the application and verify that the correct controller and actions are registered for them - without having to worry about any side-effects:

Figure 26


View Entire Article

User Comments

Title: The second routing rule above is added to special-case the root "Default.aspx" URL in our application (which is sometimes passed by web servers in pla   
Name: The second routing rule above is added to special-case the root "Default.aspx" URL in our application (which is sometime
Date: 2009-02-12 4:24:01 AM
Comment:
The second routing rule above is added to special-case the root "Default.aspx" URL in our application (which is sometimes passed by web servers in place of "/" when handling requests for the root URL of an application). This rule ensures that requests for either the root "/Default.aspx" or "/" to our application are handled by the "Index()" action on the "HomeController" class (which is a controller automatically added by Visual Studio when we created a new application using the "ASP.NET MVC Web Application" project templateThe second routing rule above is added to special-case the root "Default.aspx" URL in our application (which is sometimes passed by web servers in place of "/" when handling requests for the root URL of an application). This rule ensures that requests for either the root "/Default.aspx" or "/" to our application are handled by the "Index()" action on the "HomeController" class (which is a controller automatically added by Visual Studio when we created a new application using the "ASP.NET MVC Web Application" project templateThe second routing rule above is added to special-case the root "Default.aspx" URL in our application (which is sometimes passed by web servers in place of "/" when handling requests for the root URL of an application). This rule ensures that requests for either the root "/Default.aspx" or "/" to our application are handled by the "Index()" action on the "HomeController" class (which is a controller automatically added by Visual Studio when we created a new application using the "ASP.NET MVC Web Application" project templateThe second routing rule above is added to special-case the root "Default.aspx" URL in our application (which is sometimes passed by web servers in place of "/" when handling requests for the root URL of an application). This rule ensures that requests for either the root "/Default.aspx" or "/" to our application are handled by the "Index()" action on the "HomeController" class (which is a controller aut
Title: MVC website   
Name: Harendra chauhan
Date: 2009-01-27 2:15:12 AM
Comment:
I have ASP .net MVC website created on my machine ,it is runnig fine on my machine ,all links are working but when I hosted is on IIS 5.1 that time home page is assible from other pc on network but all its Links(url routing) is not working,Images are not getting loaded , css is not working.. Plz provide the help if a any... How can make my web app workable ?
Title: Problem Routing Solution   
Name: AMS
Date: 2008-10-30 2:27:16 PM
Comment:
lobo,

URL encode the product id when it goes in the url then decode it on the way back out. Should clean it up for you
Title: Problem routing   
Name: Lobo
Date: 2008-09-29 2:30:48 PM
Comment:
If you have products id like "7*"
The url used to view this product: /Products/Detail/7*
But this url generate an error... "Illegal characters in path"

How can i resolve this?

Product Spotlight
Product Spotlight 





Community Advice: ASP | SQL | XML | Regular Expressions | Windows


©Copyright 1998-2024 ASPAlliance.com  |  Page Processed at 2024-10-06 3:22:15 PM  AspAlliance Recent Articles RSS Feed
About ASPAlliance | Newsgroups | Advertise | Authors | Email Lists | Feedback | Link To Us | Privacy | Search