我正在尝试编写一个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
抓住。
答案 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();
}