我如何在启动文件上配置基于属性的路由,而不是在 asp.net 核心 mvc 中使用控制器操作上的属性

How can i configure attribute based routing on startup file instead of using attribute on controller action in asp.net core mvc

[Route("name/{code?}")]
public IActionResult Index(string Code)
{
    return RedirectToAction("Index", "Home", new { code = Code });
}   

如何在启动文件配置方法中配置此路由?

您可以试试下面的代码:

app.UseEndpoints(endpoints =>
        {

            endpoints.MapControllerRoute(
                name: "Demo",
                pattern: "name/{code?}",
                defaults: new { controller = "Home", action = "Index" });
            endpoints.MapControllerRoute(
                name: "default",
                pattern: "{controller=Home}/{action=Index}/{id?}");

        });