routing

There are 25 entries for the tag routing

RouteDebugger 2.0

I’m at Mix11 all week and this past Monday, I attended the Open Source Fest where multiple tables were set up for open source project owners to show off their projects. One of  my favorite projects is also a NuGet package named Glimpse Web Debugger. It adds a FireBug like experience for grabbing server-side diagnostics from an ASP.NET MVC application while looking at it in your browser. It provides a browser plug-in like experience without the plug-in. One of the features of their plug-in is a route debugger inspired by my route debugger. Over time, as Glimpse catches...

Routing Regression With Two Consecutive Optional Url Parameters

It pains me to say it, but ASP.NET MVC 3 introduces a minor regression in routing from ASP.NET MVC 2. The good news is that there’s an easy workaround. The bug manifests when you have a route with two consecutive optional URL parameters and you attempt to use the route to generate an URL. The incoming request matching behavior is unchanged and continues to work fine. For example, suppose you have the following route defined: routes.MapRoute("by-day", "archive/{month}/{day}", ...

Redirecting Routes To Maintain Persistent URLs

Over a decade ago, Tim Berners-Lee, creator of the World Wide Web instructed the world know that cool URIs don’t change with what appears to be a poem, but it doesn’t rhyme and it’s not haiku. What makes a cool URI? A cool URI is one which does not change. What sorts of URI change? URIs don't change: people change them. In a related article, URL as UI, usability expert Jakob Nielsen lists the following criteria for...

Introducing RouteMagic

Over the past couple of years, I’ve written several blog posts on ASP.NET Routing where I provided various extensions to routing. Typically such blog posts included a zip download of the binaries and source code to allow readers to easily try out the code. But that’s always been a real pain and most people don’t bother. But now, there’s a better way to share such code. Moving forward, I’ll be using NuGet packages as a means of sharing my code samples. In the case of my routing extensions, I’ve compiled them into a solution I call RouteMagic (source...

Grouping Routes Part 2

In part 1 of this series, we looked at the scenario for grouping routes and how we can implement matching incoming requests with a grouped set of routes. In this blog post, I’ll flesh out the implementation of URL Generation. Url Generation Implementation URL generation for a group route is tricky, especially when using named routes because the individual routes that make up the group aren’t in the main route collection. As I noted before, the only route that’s actually added to the route table is the GroupRoute. Thus if you supply a route name for...

ASP.NET MVC 3 Extensionless URLs on IIS 6

A lot has been written about how to get ASP.NET MVC running on IIS 6 with extensionless URLs. Up until now, the story hasn’t been very pretty. When running ASP.NET MVC on ASP.NET 4, it gets a lot easier. To be fair, the part that makes it easier has nothing to do with ASP.NET MVC 3 and everything to do with a little known new feature of ASP.NET 4 creatively called the ASP.NET 4 Extensionless URL feature. ASP.NET MVC 3 requires ASP.NET 4 so it naturally benefits from this new feature. If you have a server running IIS...

Grouping Routes Part 1

UPDATE: 2011/02/13: This code is now included in the RouteMagic NuGet package! To use this code, simply run Install-Package RouteMagic within the NuGet Package Manager Console. One thing ASP.NET Routing doesn’t support is washing and detailing my car. I really pushed for that feature, but my coworkers felt it was out of scope. Kill joys. Another thing Routing doesn’t support out of the box is a way to group a set of routes within another route. For example, suppose I want a set of routes to all live under the same URL path. Today, I’d need to make sure all...

Getting The Route Name For A Route

A question I often receive via my blog and email goes like this: Hi, I just got an email from a Nigerian prince asking me to hold some money in a bank account for him after which I’ll get a cut. Is this a scam? The answer is yes. But that’s not the question I wanted to write about. Rather, a question that I often see on StackOverflow and our ASP.NET MVC forums is more interesting to me and it goes something like this: How do I get the route...

Named Routes To The Rescue

The beginning of wisdom is to call things by their right names – Chinese Proverb Routing in ASP.NET doesn’t require that you name your routes, and in many cases it works out great. When you want to generate an URL, you grab this bag of values you have lying around, hand it to the routing engine, and let it sort it all out. For example, suppose an application has the following two routes defined routes.MapRoute( name: "Test", ...

Overriding a .svc Request With Routing

I was drawn to an interesting question on StackOverflow recently about how to override a request for a non-existent .svc request using routing. One useful feature of routing in ASP.NET is that requests for files that exist on disk are ignored by routing. Thus requests for static files and for .aspx and .svc files don’t run through the routing system. In this particular scenario, the developer wanted to replace an existing .svc service with a call to an ASP.NET MVC controller. So he deletes the .svc file and adds the following route: ...

Putting the Con (COM1, LPT1, NUL, etc.) Back in your URLs

One annoyance that some developers have run into with ASP.NET MVC is that certain reserved filenames are not allowed in URLs. Often, this is manifested as a Bad Request error or a File Not Found (404) error. The specifics of this restriction are accounted for in an interesting blog post entitled Zombie Operating Systems and ASP.NET MVC. This actually wasn’t a restriction on ASP.NET MVC but was built into the core of ASP.NET itself. Fortunately, ASP.NET 4 fixes this issue with a new setting. In web.config, simply add <httpRuntime relaxedUrlToFileSystemMapping="true"/> to the system.web node. Here’s a snippet from my...

ASP.NET MVC 2 Optional URL Parameters

If you have a model object with a property named Id, you may have run into an issue where your model state is invalid when binding to that model even though you don’t have an “Id” field in your form. The following scenario should clear up what I mean. Suppose you have the following simple model with two properties. public class Product { public int Id { get; set; } public string Name { get; set; } } And you are creating a view...

Ambiguous Controller Names With Areas

Note: This describes the behavior of ASP.NET MVC 2 as of the release candidate. It’s possible things might change for the RTM. When using areas in ASP.NET MVC 2, a common problem you might encounter is this exception message. The controller name 'Home' is ambiguous between the following types: AreasDemoWeb.Controllers.HomeController AreasDemoWeb.Areas.Blogs.Controllers.HomeController This message is telling you that the controller factory found two types that match the route data for the current request. Typically this happens when you have a controller of the same name in an area and in the main project. For example, in the screenshot below, notice that we have...

A RouteHandler for IHttpHandlers

This code has been incorporated into a new RouteMagic library I wrote which includes Source Code on CodePlex.com as well as a NuGet package! I saw a bug on Connect today in which someone offers the suggestion that the PageRouteHandler (new in ASP.NET 4) should handle IHttpHandler as well as Page. I don’t really agree with the suggestion because while a Page is an IHttpHandler, an IHttpHandler is not a Page. What I this person really wants is a new handler specifically for http handlers. Let’s give it the tongue twisting name: IHttpHandlerRouteHandler. Unfortunately, it’s too late to add...

Routing for Web Forms in ASP.NET 4.0

A while back on a lark, I posted a prototype demonstrating how one could use Routing within Web Forms. This is something you can do today with ASP.NET 3.5 SP1, because of the work we did to separate Routing from ASP.NET MVC. I would have liked to include Web Form Routing as part of the Routing feature when we were working on SP1, but we didn’t have the time to do so in a robust manner before SP1 was locked down. Since then, Scott Galloway, who just happens to be my office mate, has taken the reigns and is...

Redirect Routes and other Fun With Routing And Lambdas

ASP.NET Routing is useful in many situations beyond ASP.NET MVC. For example, I often need to run a tiny bit of custom code in response to a specific request URL. Let’s look at how routing can help. First, let’s do a quick review. When you define a route, you specify an instance of IRouteHandler associated with the route which is given a chance to figure out what to do when that route matches the request. This step is typically hidden from most developers who use the various extension methods such as MapRoute. Once the route handler makes...

A Case Study In Design Tradeoffs: Usability vs Discoverability

Usability and Discoverability (also referred to as Learnability) are often confused with one another, but they really are distinct concepts. In Joel Spolsky’s wonderful User Interface Design for Programmers (go read it!), Joel provides an metaphor to highlight the difference. It takes several weeks to learn how to drive a car. For the first few hours behind the wheel, the average teenager will swerve around like crazy. They will pitch, weave, lurch, and sway. If the car has a stick shift they will stall the engine in the middle of busy intersections in a truly terrifying...

How a Method Becomes An Action

This is one of them “coming of age” stories about how a lowly method becomes a full fledged Action in ASP.NET MVC. You might think the two things are the same thing, but that’s not the case. It is not just any method gets to take the mantle of being an Action method. Routing Like any good story, it all begins at the beginning with Routing. By default, one of the routes defined in the MVC project template has the following URL pattern: {controller}/{action}/{id} When a request comes...

Make Routing Ignore Requests For A File Extension

By default, ASP.NET Routing ignores requests for files that do not exist on disk. I explained the reason for this in a previous post on upcoming routing changes. Long story short, we didn’t want routing to attempt to route requests for static files such as images. Unfortunately, this caused us a headache when we remembered that many features of ASP.NET make requests for .axd files which do not exist on disk. To fix this, we included a new extension method on RouteCollection, IgnoreRoute, that creates a Route mapped to the StopRoutingHandler route handler (class that implements IRouteHandler). Effectively,...

Updated Routing With WebForms

A while back I wrote a sample that demonstrated how to use Routing with WebForms. If you missed it, you can download the code here With the recent announcement that Routing will be included with .NET 3.5 SP1, you can see why I wanted to put that demo together. I have since updated that sample to work with the versions of Routing that comes with the April CodePlex build of MVC. This should also work with the SP1 Beta. I’ll verify that when I get a moment. As part of this update, I added a new...

RouteEvaluator For Unit Testing Routes

A while back I wrote a routing debugger which is useful for testing your routes and seeing which routes would match a given URL. Rob suggested we have something like this for unit tests, so I whipped something simple up. This is a class that allows you to test multiple different URLs quickly. You simply create the RouteEvaluator giving it a collection of routes and then GetMatches which returns a List<RouteData> containing a RouteData instance for every route that matches, not just the first one. Here's a sample of usage. [Test] public void CanMatchUsingRouteEvaluator() { var routes = new RouteCollection(); GlobalApplication.RegisterRoutes(routes); var...

Upcoming Changes In Routing

Made a few corrections on having default.aspx in the root due to a minor bug we just found. Isn’t preview code so much fun? We’ve been making some changes to routing to make it more powerful and useful. But as Uncle Ben says, with more power comes more responsibility. I’ll list out the changes first and then discuss some of the implication of the changes. Routes no longer treat the . character as a separator. Currently, routes treat the . and / characters as special. They are separator characters. The upcoming release of routing will only treat the...

ASP.NET Routing Debugger

UPDATE: I’ve added a NuGet package named "routedebugger" to the NuGet feed, which will make it much easier to install. In Scott Hanselman’s wonderful talk at Mix, he demonstrated a simple little route tester I quickly put together. This utility displays the route data pulled from the request of the current request in the address bar. So you can type in various URLs in the address bar to see which route matches. At the bottom, it shows a list of all defined routes in your application. This allows you to see which of your routes would match the current URL. The reason this...

Using Routing With WebForms

UPDATE: I updated the sample to work with the final version of ASP.NET Routing included with ASP.NET 3.5 SP1. This sample is now being hosted on CodePlex. Download the demo here In my last post I described how Routing no longer has any dependency on MVC. The natural question I’ve been asked upon hearing that is “Can I use it with Web Forms?” to which I answer “You sure can, but very carefully.” Being on the inside, I’ve had a working example of this for a while now based on early access to the bits. Even so, Chris Cavanagh impressively beats me to...

Testing Routes In ASP.NET MVC

The ASP.NET Routing engine used by ASP.NET MVC plays a very important role. Routes map incoming requests for URLs to a Controller and Action. They also are used to construct an URL to a Controller/Action. In this way, they provide a two-way mapping between URLs and controller actions. When building routes, it may be useful to write unit tests for the routes to ensure that you’ve set up the proper mappings you intend. ScottGu touched a bit on unit testing routes in part 2 of his series on MVC in which he covers URL Routing. In this post,...