簡體   English   中英

如何在啟動文件上配置基於屬性的路由,而不是在 asp.net 核心 mvc 中使用 controller 動作上的屬性

[英]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?}");

        });

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM