c# - URL 只是没有显示我想要的

标签 c# asp.net-mvc url asp.net-mvc-3

<分区>

Possible Duplicate:
How can I accomplish this type of URL in ASP.Net MVC2?

我想创建如下 HTML 链接:

/Auctions/Clothes
/Auctions/Electronics
/Auctions/Real Estate

这是我构建链接的方式:

<li>@Html.ActionLink("Ropa", "Index", "Anuncios", new { category = "Ropa" }, new { })</li>
<li>@Html.ActionLink("Libros", "Index", "Anuncios", new { category = "Libros" }, new { })</li>

问题是链接是这样使用的:

http://localhost:8589/Anuncios?category=Libros

我希望我的 URLS 看起来很漂亮,因此我希望上面的内容是这样的:

/Anuncios/Libros

关于如何解决这个问题有什么建议吗?这是 ActionResult 方法,澄清一下,这正是我想要它做的。它有效,除了 URL 很糟糕。

public ActionResult Index(string category)
{            
    AuctionRepository auctionRepo = new AuctionRepository();
    var auctions = auctionRepo.FindAllAuctions().Where(a => a.Subcategory.Category.Name == category);
    return View(auctions);
}

最佳答案

请查看此问题以实现您的要求。

编辑:

创建一个新的 HTML 帮助程序,您将使用它来呈现这样的菜单

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

最后在你的 _Layout 页面中像这样使用它

@Html.MyMenu

关于c# - URL 只是没有显示我想要的,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4434781/

相关文章:

javascript - 如何通过ajax传递包含数据和文件的对象数组并在mvc Controller 中检索?

asp.net-mvc - 带有迁移的 ASP.NET MVC 6 + EF6

c# - 如何在局部 View 中使用模型验证

php - opencart 中的 URL 重写

c# - 需要将字符串拆分为子字符串但不能使用拆分

c# - 关闭 Radwindow 时在父页面上重新加载组合框

JAVA获取URL API

url - Encore webpack 或 Assets 在 symfony 4 中生成错误的图像路径

C# MVC Controller 重定向到 Tel、SMS 和 mailto 链接

c# - 我的线程池做错了什么?