views:

902

answers:

4

I get a 404 error when I navigate to the following URL using the route below:

http://localhost:53999/properties/

However, all the following are correctly routed to the List action in my controller:

http://localhost:53999/properties/usa/new-york/manhattan/12

http://localhost:53999/properties/usa/new-york/manhattan

http://localhost:53999/properties/usa/new-york

http://localhost:53999/properties/usa

public static void RegisterRoutes(RouteCollection routes)
{
    routes.IgnoreRoute("{resource}.axd/{*pathInfo}");

    //properties
    routes.MapRoute(
        "Properties",
        "Properties/{country}/{state}/{city}/{id}",
        new
        {
            controller = "Properties",
            action = "List",
            country = UrlParameter.Optional,
            state = UrlParameter.Optional,
            city = UrlParameter.Optional,
            id = UrlParameter.Optional
        } 
    );

    //default
    routes.MapRoute(
        "Default",                                              // Route name
        "{controller}/{action}/{id}",                           // URL with parameters
        new { controller = "Home", action = "Index", id = "" }  // Parameter defaults
    );
}

In PropertiesController.cs:

public ActionResult List(string country, string state, string city, string id)
{
     return View();
}

Anyone know what I'm missing? Looks like it should just go to the default action, but it obviously doesn't...

A: 

Instead of passing

(string)null

try passing

UrlParameter.Optional

as specified in Phil Haacks post here. I don't know if this will solve the issue as I'm not currently in a position to test it.

Simon G
That's not available in MVC 1.0 though.
çağdaş
Please note that UrlParameter.Optional is only available in MVC 2 RC 2, which was just released in early February.
Brettski
@çağdaş: The OP specifically says MVC 2.
Dan Atkinson
@Dan Atkinson Ah, I read the post body again before commenting, but apparently not the title.
çağdaş
I tried it with UrlParameter.Optional (and that's how I'll do it from now on, thanks Simon), but it's still returning a 404 when I go to http://localhost:53999/properties/
Ozzie Perez
+1  A: 

You can also try the following (since you're using MVC 1.0).

Add a route above the current route:

routes.MapRoute(
    "Properties", "Properties", new { controller = "Properties", action = "List"} 
);

And add an overloaded ActionResult List() method to your controller:

public ActionResult List()
{
     return View();
}
George Stocker
I too thought he was using MVC 1.0 :) But he says MVC 2 in the title. It's weird we both missed that.
çağdaş
I tried it exactly as you have it, but VS complained I already had a route named "Properties", so I changed it to "DefaultProperties" to give it something different. Unfortunately, I still get a 404 when I simply go to localhost:53999/properties. However, once I have a value for one of the optional parameters, it gets routed to the action no problem.P.S. Yeah, I'm using MVC 2 RC2..
Ozzie Perez
+1  A: 

Have you tried this Route Debugger from Phil Haack? It may help you determine what is going on.

Brettski
Thanks, yea actually I just tried it and it seems to recognize the correct controller and action, but it still throws a 404. It may be a bug, cause I was able to fix it by changing the leading route keyword from "Properties" to anything else. It could be that the keyword is reserved or something... dunno. I tested the entire thing with a different leading routing keyword and it works just fine. Weird, but it's working now. :)
Ozzie Perez
A: 

Brad Wilson answered it on this post: http://forums.asp.net/p/1527697/3690295.aspx#3690295

"No, the problem is that you have a Properties folder on your disk, so it's dispatching through the standard dispatcher (not MVC), and then 404ing because it can't find a default document."

Ozzie Perez