0

我有一个名为的控制器About和一个名为Index. 我希望 URL 是这样的(动作名称将是动态的):

www.example.com/about/aaa
www.example.com/about/bbb
www.example.com/about/ccc

路由

routes.MapRoute(
    name: "About",
    url: "{controller}/{name}",
    defaults: new { controller = "About", action = "Index"}

控制器

public class AboutController : Controller
{
    // GET: /About/
    public ActionResult Index(string name)
    {
        return View();
    }
}

看法

@{
    ViewBag.Title = "Index";
}

<h2>Index About</h2>
4

3 回答 3

4

这应该有效。

routes.MapRoute(
    name: "About",
    url: "About/{name}",
    defaults: new
    {
        controller = "About",
        action = "Index"
    });

确保您的默认路由存在并且在关于路由之后

于 2013-11-19T08:55:11.120 回答
0
routes.MapRoute(
    name: "About",
    url: "about/{name}/{id}",
    defaults: new { controller = "About", action = "Index", id=UrlParameter.Optional}
于 2013-11-19T08:54:45.700 回答
0

您可以将ActionResult名称作为参数传递:

public ActionResult Index(string name)
{
    return View(name);
}    

public ActionResult First()
{
    return View();
}

public ActionResult Second()
{
    return View();
}

在视图中:

@Html.ActionLink("Get Action named Firts" "Index", "Home", new {name = "First"}, null)
于 2013-11-19T08:57:10.730 回答