2

我正在尝试编写一个 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

4

1 回答 1

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();
}
于 2012-10-26T08:36:14.530 回答