首页 > 解决方案 > 控制器位于区域中的 RedirectToAction

问题描述

我开始使用 ASP.Net 核心 MVC 3.1 并有一个名为Foo的区域:

endpoints.MapAreaControllerRoute(
    name: "Foo",
    areaName: "Foo",
    pattern: "{area:exists}/{controller=Home}/{action=Index}/{id?}"
);

在这个区域中,一个名为Test的控制器带有一个Index操作:https://localhost:44390/Foo/Test/- 这很好用。

我正在尝试从另一个控制器重定向到这个

return RedirectToAction("Index", "Test", new { area = "Foo" });

但这是送我去https://localhost:44390/Test?area=Foo

我如何使用RedirectToAction()以结束https://localhost:44390/Foo/Test/

标签: c#asp.net-coreasp.net-core-mvc

解决方案


当放置在默认路由之后时,我会重现您的问题。MapAreaControllerRoute因此,要解决它,您的区域路由配置必须放在第一位。

 app.UseEndpoints(endpoints =>
        {
            endpoints.MapAreaControllerRoute(
               name: "Foo",
               areaName: "Foo",
               pattern: "{area:exists}/{controller=Home}/{action=Index}/{id?}"
           );
            endpoints.MapControllerRoute(
                name: "default",
                pattern: "{controller=Home}/{action=Index}/{id?}");

        });

推荐阅读