URL just isn't displaying what I want [duplicate]
Possible Duplicate:
How can I accomplish this type of URL in ASP.Net MVC2?
I want to create HTML links like:
/Auctions/Clothes
/Auctions/Electronics
/Auctions/Real Estate
Here's how I'm constructing the links:
开发者_高级运维<li>@Html.ActionLink("Ropa", "Index", "Anuncios", new { category = "Ropa" }, new { })</li>
<li>@Html.ActionLink("Libros", "Index", "Anuncios", new { category = "Libros" }, new { })</li>
The problems is the links are being used like this:
http://localhost:8589/Anuncios?category=Libros
I want my URLS to be nice looking, as such I'd like the above to be like:
/Anuncios/Libros
Any suggestions on how to fix this? Here's the ActionResult method, and to clarify, this is doing exactly what I want it to do. It works, except the URL is horrible.
public ActionResult Index(string category)
{
AuctionRepository auctionRepo = new AuctionRepository();
var auctions = auctionRepo.FindAllAuctions().Where(a => a.Subcategory.Category.Name == category);
return View(auctions);
}
please have a look at this question for implementing your requirement.
- MVC 2.0 dynamic routing for category names in an e-store
EDIT:
create a new HTML helper that you will use to render the menu like this
public static class MyMenuHelper {
public static string MyMenu(this HtmlHelper helper) {
List<Category> categories = GetCategories();
foreach ( Category c in categories ) {
helper.RouteLink( c.Name, "AuctionCategoryDetails", new { categoryName = c.Name } );
}
}
}
Finally in your _Layout page use it like this
@Html.MyMenu
You need to add some routes so MVC knows how you would prefer the links to look like. Its not like it can read your thoughts, or make coffee.
public static void RegisterRoutes(RouteCollection routes)
{
routes.MapRoute(
"Anuncios", // Route name
"Anuncios/{category}", // URL with parameters
new { category = "Index" } // Parameter defaults
);
}
精彩评论