views:

21

answers:

2

I want to add a simple route to my webapp, but it just doesn't work and i don't know why

code in global.asax.cs:

routes.MapRoute(
            "BrowseGenre",
            "{controller}/{action}/{genre}",
            new {controller = "Store", action = "Browse", genre = UrlParameter.Optional}
        );

code in StoreController.cs:

    // GET: /Store/Browse
    public ActionResult Browse(string genreName)
    {

        var genreModel = storeDB.Genres.Include("Albums").Single(g => g.Name == genreName);
        //....

genreName is always null

the actionlinks are generated correctly (like domain.com/Store/Browse/Jazz

+2  A: 

The action parameter should be called genre instead of genreName (the same way it uis called in your route):

public ActionResult Browse(string genre)
Darin Dimitrov
@Rup, yes correct.
Darin Dimitrov
A: 

change your anonymous type from

 new {controller = "Store", action = "Browse", genre = UrlParameter.Optional}

to

 new {controller = "Store", action = "Browse", genreName = UrlParameter.Optional}

OR

change the argument name in action from "genreName" to "genre" so that name of the member in the anonymous type matches the name of the argument in your action method.

DevilDog74