动态MVC路由:操作名称

问题描述 投票:0回答:3

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

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

Routing

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

Controller

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

查看

@{
    ViewBag.Title = "Index";
}

<h2>Index About</h2>
asp.net-mvc asp.net-mvc-4 asp.net-mvc-routing mvcroutehandler
3个回答
4
投票
这应该起作用。

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

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

0
投票
routes.MapRoute( name: "About", url: "about/{name}/{id}", defaults: new { controller = "About", action = "Index", id=UrlParameter.Optional}

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)

最新问题
© www.soinside.com 2019 - 2025. All rights reserved.