Ozzie Perez
Ozzie Perez

Reputation: 583

ASP.Net MVC 2 RC2: Custom route returns 404 when all optional parameters are null

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...

Upvotes: 1

Views: 2622

Answers (4)

Ozzie Perez
Ozzie Perez

Reputation: 583

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."

Upvotes: 0

Brettski
Brettski

Reputation: 20081

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

Upvotes: 1

George Stocker
George Stocker

Reputation: 57872

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();
}

Upvotes: 2

lancscoder
lancscoder

Reputation: 8768

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.

Upvotes: 0

Related Questions