简体   繁体   English

重定向到区域操作中的操作

[英]Redirect to action in area actions

I have an area which has a controller with some actions.我有一个区域有一个 controller 有一些动作。

the controller name is home . controller 名称是home

the action which has the problem looks like below:有问题的操作如下所示:

 public IActionResult Action1()
 {
         some code ...

        return RedirectToAction("Action2");
 }

Action2 looks like: Action2 看起来像:

 public IActionResult Action2()
 {
         some code ...

        return View();
 }

the problem is that it redirects to the mentioned action but it doesn't write the area name before the controller name问题是它重定向到上述操作,但它没有在 controller 名称之前写入区域名称

url should be => MyArea/home/action2 url 应该是 => MyArea/home/action2

but it is => home/action2但它是 => home/action2

which makes an 500 error.这会产生 500 错误。

and my startup is:我的创业公司是:

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

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

does any have any solution?有什么解决办法吗?

thanks in advance.提前致谢。

1 - Change RedirectToAction to 1 - RedirectToAction更改为

return RedirectToAction("Action2", "Home", new { area = "MyArea" });

2 - Your route configuration must be like this and you must register area before default controller 2 - 您的路线配置必须是这样的,您必须在默认 controller之前注册区域

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

3 - On the top of the controller you must add [Area("MyArea")] attribute 3 - 在 controller 的顶部,您必须添加[Area("MyArea")]属性

[Area("MyArea")]
public class HomeController : Controller
{
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM