路由捕获出错

时间:2012-10-26 07:55:22

标签: c# asp.net-mvc routes

我正在尝试编写一个C#ASP.NET应用程序,我遇到了路由问题,我不知道如何继续前进:

routes.MapRoute(
    "DefaultArticole",
    "{limba}/Art-{id}/{titluPagina}",
    new { controller = "Pagini", action = "Index", limba = UrlParameter.Optional, id = UrlParameter.Optional, titluPagina = UrlParameter.Optional }
);

routes.MapRoute(
    "DefaultPagini",
    "{limba}/{paginaCurenta}/{id}",
    new { controller = "Home", action = "Index", limba = UrlParameter.Optional, paginaCurenta = UrlParameter.Optional, id = UrlParameter.Optional }
);

我想要的只是在uri上:

/Art-10/title

/en/Art-10/title

DefaultArticole抓住。

1 个答案:

答案 0 :(得分:0)

最简单的方法是为Pagini控制器定义两条路线:

routes.MapRoute("DefaultArticole", "{limba}/Art-{id}/{titluPagina}",
    new { controller = "Pagini", action = "Index", limba = UrlParameter.Optional, id = UrlParameter.Optional, titluPagina = UrlParameter.Optional });

routes.MapRoute("DefaultArticoleDefaultLimba", "Art-{id}/{titluPagina}",
    new { controller = "Pagini", action = "Index", id = UrlParameter.Optional, titluPagina = UrlParameter.Optional });

然后在PaginiController你可以这样做:

public ActionResult Index(string limba, int id, string titluPagina)
{
    // Set the language to default, if none given (via /Art-10/title route)
    if (string.IsNullOrEmpty(limba)) limba = "en";
    // Your actual stuff here
    return View();
}