【问题标题】:.netCore 5 Area route Not working in RedirectToActionArea and go as a query string.net Core 5 区域路由在 RedirectToAction 区域不起作用并作为查询字符串
【发布时间】:2021-08-31 16:10:50
【问题描述】:

当我在asp.net core 5中使用下面的命令时

return RedirectToAction("Index", "SystemUsersManagement", new { area = "admin" });

它不能正常工作并获得Status Code: 404; Not Found Error。和 Area 作为查询字符串,如

https://localhost:44322/SystemUsersManagement/Index?area=admin

哪里错了。

我的控制器:

[Area("Admin")]
public class SystemUsersManagementController : Controller
{
    public ActionResult Save()
    {
       //do something
        return RedirectToAction("Index", "SystemUsersManagement", new { area = "admin" });
    }
}

我的启动路线:

app.UseMvc(routes =>
            {
                routes.MapRoute(
            name: "areas",
            template: "{area:exists}/{controller=Home}/{action=Index}/{id?}"
          );
                routes.MapRoute(
                    name: "Default",
                    template: "{controller}/{action}/{id?}",
                    defaults: new { controller = "Home", action = "Login" }
                );
                routes.MapRoute("DefaultApiWithId", "Api/{controller}/{id?}");
                routes.MapRoute("DefaultApiWithAction", "Api/{controller}/{action}");
                routes.MapRoute("DefaultApiGet", "Api/{controller}", new { action = "Get" });
                routes.MapRoute("DefaultApiPost", "Api/{controller}", new { action = "Post" });
            });

也在 ConfigureService 中 . .

services.AddMvc(option=>option.EnableEndpointRouting=false).AddJsonOptions(options => {
                options.JsonSerializerOptions.PropertyNameCaseInsensitive = true;
                options.JsonSerializerOptions.PropertyNamingPolicy = null;
            });

【问题讨论】:

  • 嗨@Morteza Jangjoo,有什么更新吗?我的回答解决了你的问题吗?如果没有,请跟进。如果是这样,请记住接受作为答案。
  • 嗨@Rena。不幸的是我的问题没有解决
  • 嗨@Morteza Jangjoo,它在我的项目中运行良好,所以您的项目似乎仍然包含您可能无法分享的任何其他问题。我建议您可以创建一个新项目并按照我的项目结构进行测试。如果你能分享一个简单的 repo 会更有帮助。

标签: asp.net-core routes area


【解决方案1】:

请务必在您的控制器中添加[Area] 属性,如下所示:

[Area("Admin")]
public class SystemUsersManagementController : Controller
{
    public ActionResult Index()
    {
        return View();
    }
}

项目结构应如下所示:

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2019-03-05
    • 2017-01-22
    • 1970-01-01
    • 1970-01-01
    • 2011-09-14
    • 2018-09-01
    • 2013-12-26
    • 2023-01-27
    相关资源
    最近更新 更多