Routing table for /Store/Details/id/abc

Aug 15, 2010 at 4:47 PM

Hello!

 

I have problem with links to actions.

Store action (store controller) generate list of links:

http://localhost:XYZ/Store/Browse/Jazz

Deatails action (store controller) generate list of links:

http://localhost:XYZ/Store/Details?id=563

How to get links in that format:

http://localhost:XYZ/Store/Details/id/563

 

Another problem is, the simplest way to nicely catch errors.

 

I made another action (with new model class BadGenre with only one property - Title):

public ActionResult BrowseUnknown(string genre)
{
var model = new BadGenre { Title = genre };
return View(model);
}

and changed the existing one:

public ActionResult Browse(string genre)
{
try
{
var genreModel = this.storedb.Genres.Include("Albums")
.Single(g => g.Name == genre);
var viewModel = new StoreBrowseViewModel { Genre = genreModel, Albums = genreModel.Albums.ToList() };
return View(viewModel);
}
catch (Exception ex)
{
return RedirectToAction(Names.BROWSE_UNKNOWN, Names.STORE, new { genre });
}
}

When 'genre' is not in database (or album - BadAlbum with one property: id), action is redirected to page with message, that 'genre' (or album) does not exist.

But this  require additional action, additional model (model view class) and additional aspx page.

Best regards

Aug 15, 2010 at 7:19 PM
Edited Aug 15, 2010 at 7:20 PM

I found the answer for the first question.

The routing table:

 

routes.MapRoute(
"Store", // Route name
"Store/Browse/{genre}", // URL with parameters
new { controller = "Store", action = "Browse", genre = UrlParameter.Optional } // Parameter defaults
);
routes.MapRoute(
"StoreDetails", // Route name
"Store/Details/{id}", // URL with parameters
new { controller = "Store", action = "Details", id = UrlParameter.Optional } // Parameter defaults
);
routes.MapRoute(
"Default", // Route name
"{controller}/{action}/{id}", // URL with parameters
new { controller = "Home", action = "Index", id = UrlParameter.Optional } // Parameter defaults
);

Oct 19, 2010 at 9:30 PM
Edited Oct 19, 2010 at 9:39 PM

Hi!

i wanna show the Detail Info in this way:  
{genre}/{id}/{title}   (http://localhost:26641/Rock/610/Temple-of-the-Dog)

i tried to do something like:
routes.MapRoute( 
	"StoreDetails", // Route name 
	"{genre}/{id}/{title}", // URL with parameters 
	new { controller = "Store", action = "Details", genre = UrlParameter.Optional, id = UrlParameter.Optional, title = UrlParameter.Optional } // Parameter defaults 
	);

and in the browse.aspx putted this:

 <a href="<%: Url.Action("Details", new {genre = album.Genre, id = album.AlbumId, title = album.Title.Replace(' ','-')}) %>">
 	<img alt="<%: album.Title %>" src="<%: album.AlbumArtUrl %>" /> 
	<span><%: album.Title %></span> 
</a>

but i didnt work, i am receiving the following error:
Server Error in '/' Application.
The parameters dictionary contains a null entry for parameter 'id' of non-nullable type 'System.Int32' for method 'System.Web.Mvc.ActionResult Details(Int32)' in 'MvcMusicStore.Controllers.StoreController'. An optional parameter must be a reference type, a nullable type, or be declared as an optional parameter.Parameter name: parameters