简体   繁体   English

MVC Routing .net Core

[英]MVC Routing .net Core

In my earlier .net application I used to have the below routing in the route.config 在我早期的.net应用程序中,我曾经在route.config中使用以下路由

routes.MapRoute(
name: "Default",
url: "{tenant}/{controller}/{action}/{id}",
defaults: new { tenant = "GRE", controller = "Home", action = "Index", id = UrlParameter.Optional }
);

Now I am moving to .Net Core and somehow I am not able to setup the similar routing in the startup.cs 现在我转移到.Net Core,不知怎的,我无法在startup.cs中设置类似的路由

I have tried as below this but it never hits. 我尝试过如下,但它从未打过。

 app.UseMvc(routes =>
            {  
                routes.MapRoute("testroute", "{tenant}/{controller}/{action}/{id}",
                        defaults: new { tenant = "GRE", controller = "Home", action = "Index" });

            });

HomeController.cs HomeController.cs

public IActionResult Index(string tenant)
        {
            return View();
        }

Appreciate any sort of help or hints on how to make it work 欣赏任何有关如何使其工作的帮助或提示

Thanks, 谢谢,

Your id parameter, which is optional your asp.net mvc code, is not optional in asp.net core code and have no default, for that reason it does not match. 你的id参数,你的asp.net mvc代码是可选的,在asp.net核心代码中不是可选的,没有默认值,因此它不匹配。 To make it optional, add "?" 要使其可选,请添加“?” to the name, or set a default: 到名称,或设置默认值:

app.UseMvc(routes =>
{
    routes.MapRoute("testroute", "{tenant}/{controller}/{action}/{id?}",
        defaults: new { tenant = "GRE", controller = "Home", action = "Index" });

});

You can also set defaults inline, like this: 您还可以设置内联默认值,如下所示:

routes.MapRoute("testroute", "{tenant=GRE}/{controller=Home}/{action=Index}/{id?}");

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM