c# – MVC Routing .net Core
作者:互联网
在我早期的.net应用程序中,我曾经在route.config中使用以下路由
routes.MapRoute(
name: "Default",
url: "{tenant}/{controller}/{action}/{id}",
defaults: new { tenant = "GRE", controller = "Home", action = "Index", id = UrlParameter.Optional }
);
现在我转移到.Net Core,不知怎的,我无法在startup.cs中设置类似的路由
我尝试过如下,但它从未打过.
app.UseMvc(routes =>
{
routes.MapRoute("testroute", "{tenant}/{controller}/{action}/{id}",
defaults: new { tenant = "GRE", controller = "Home", action = "Index" });
});
HomeController.cs
public IActionResult Index(string tenant)
{
return View();
}
欣赏任何有关如何使其工作的帮助或提示
谢谢,
解决方法:
你的id参数,你的asp.net mvc代码是可选的,在asp.net核心代码中不是可选的,没有默认值,因此它不匹配.要使其可选,请添加“?”到名称,或设置默认值:
app.UseMvc(routes =>
{
routes.MapRoute("testroute", "{tenant}/{controller}/{action}/{id?}",
defaults: new { tenant = "GRE", controller = "Home", action = "Index" });
});
您还可以设置内联默认值,如下所示:
routes.MapRoute("testroute", "{tenant=GRE}/{controller=Home}/{action=Index}/{id?}");
标签:c,asp-net-mvc,asp-net-mvc-routing,asp-net-core,visual-studio-2017 来源: https://codeday.me/bug/20190701/1347578.html