Routing 指定asp.net核心自定义路由

Routing 指定asp.net核心自定义路由,routing,asp.net-core,Routing,Asp.net Core,我想指定一个自定义路由,如:localhost:4444/code,其中code是分配给课程的随机代码。当前默认路由强制控制器/操作/Id路由。我希望将上述内容绑定到Courses/Details/Code,但不希望在路由中指定/Courses/Details以使其简短。以下路由适用于您 app.UseMvc(routes => { routes.MapRoute( name: "default", template: "{controller=Ho

我想指定一个自定义路由,如:localhost:4444/code,其中code是分配给课程的随机代码。当前默认路由强制控制器/操作/Id路由。我希望将上述内容绑定到Courses/Details/Code,但不希望在路由中指定/Courses/Details以使其简短。

以下路由适用于您

app.UseMvc(routes =>
{
    routes.MapRoute(
        name: "default",
        template: "{controller=Home}/{action=Index}/{id?}");

    routes.MapRoute(
        name: "course_code", 
        template: "{id}",
        defaults: new { controller = "Courses", action = "Details"});
});
这些路线的地图如下:

HomeController.Index()

    localhost:4444                
    localhost:4444/home          
    localhost:4444/home/index   

CoursesController.Details("math101")

    localhost:4444/math101                 
    localhost:4444/courses/details/math101

另请参见:

以下路线适合您

app.UseMvc(routes =>
{
    routes.MapRoute(
        name: "default",
        template: "{controller=Home}/{action=Index}/{id?}");

    routes.MapRoute(
        name: "course_code", 
        template: "{id}",
        defaults: new { controller = "Courses", action = "Details"});
});
这些路线的地图如下:

HomeController.Index()

    localhost:4444                
    localhost:4444/home          
    localhost:4444/home/index   

CoursesController.Details("math101")

    localhost:4444/math101                 
    localhost:4444/courses/details/math101
另见: