【问题标题】:Generate semantic URL with asp.net mvc4 ActionLink in Razor views在 Razor 视图中使用 asp.net mvc4 ActionLink 生成语义 URL
【发布时间】:2012-09-20 01:44:08
【问题描述】:

我在面向公众的网页的控制器中有以下设置

Company -> About -> Partners(我希望以 Company/About/Partners 的身份访问) 动作方法

public about(string category)
{
  ViewBag.Category = category;
  return View();
}

链接的生成如下所示,这给了我错误的 URL

@Html.ActionLink("Partners & Investors", "About", "Company",new { Category = "Partners" },null)

错误的网址

Company/About?Category=Partners%20and%20Investors

所以问题是如何生成我想要的正确 url。我该怎么办?

【问题讨论】:

  • @AndrewBarber 嗨,安德鲁我想要一个 url Company/About/category 但它会生成 Company/About?category=value 你能提供一些输入吗

标签: asp.net-mvc url asp.net-mvc-4 asp.net-mvc-routing actionlink


【解决方案1】:

当您创建新的route 并将其放在正确的位置时,将生成automatically 的网址。

添加

类似这样的:

routes.MapRoute(
    "Category",
    "Company/About/{category}",
    new { controller = "Company", action = "About", category = "default" }
);

// default
routes.MapRoute(
    "Default", // Route name
    "{controller}/{action}/{id}", // URL with parameters
    new { controller = "Home", action = "Index", id = UrlParameter.Optional }
);

也可以看看这个链接:Advanced Routing

【讨论】:

  • 哇,它成功了。我认为在使用它们之前需要在路线图中定义路线。谢谢
猜你喜欢
  • 2011-05-20
  • 1970-01-01
  • 2014-02-09
  • 1970-01-01
  • 1970-01-01
  • 2019-09-24
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多