CareerChange
CareerChange

Reputation: 669

Html.RouteLink 404 error

Can anyone help with this problem, I have the following routelink:

 @Html.RouteLink(@item.strCountry, "WeatherCity", new {id=Regex.Replace(@item.strCountry, " ","-") })

In my RouteConfig I have:

routes.MapRoute(
            name: "WeatherCity",
            url: "Cities-In/{id}",
           defaults: new { controller = "Weather", action = "Cities", id = UrlParameter.Optional }
            );

In my controller I have:

 public PartialViewResult Cities(string id) 

also tried

public ActionResult Cities(string id)

But no matter what I do I get a 404 error.

I have tried following numerous examples the last one been:

http://davecowart.wordpress.com/2011/06/08/named-routes-in-asp-net-mvc-3/

Am I missing something simple here as I cannot get this to work, i'm using MVC4.

Any help would be appreciated

George

Upvotes: 1

Views: 254

Answers (1)

Ofer Zelig
Ofer Zelig

Reputation: 17480

I think I've found your issue (reference for readers: the "comments" conversation under the question).

You should put your routes.MapRoute() thing before the default route. Else, the default route is matched and run (and MVC stops looking further to any subsequent route rules). MVC matches your URL against the default route, but then it doesn't find a corresponding action (Cities) in the DefaultController.

Upvotes: 2

Related Questions