RedirectToAction between areas?

本秂侑毒 提交于 2019-11-27 17:16:09

Did you try this?:

return RedirectToAction("action", "controller", new { area = "area" });

Your answer was helpful to me. Just wanted to add below:

If you want to redirect from one area to another area, above code works well.

And, if you want to redirect from one area to a controller/view which is not there in the area folder (i.e. in most cases, your front end), you can specify area = "".

i.e.

return RedirectToAction("action", "controller", new { area = "" });

I would like to ask a follow-up here. Understanding that

return RedirectToAction("action", "area1/controller")

Is also represented by

return RedirectToAction("action", "controller", new { area = "area1" });

How does one drill down into further area nestings using the same notation? The big gain in having the appropriate notation is that, with reSharper in particular, refactoring is handled appropriately if you ever need to go back and 'rename' any of the controllers. Once I add an additional area to the mix and have them nested within one another, the previous notation isn't equivalent.

return RedirectToAction("action", "area1/area2/controller");

I'll keep the question updated if I find it first.


Ok, so I believe the answer isn't anything spectacular, but the best way to do it so you still get some sort of valid coloration w/ resharper & intellisense is as follows:

return RedirectToAction("action","controller", new { area = "area1/area2" });

At least here you'll have the ability to F12 or Ctrl+Click the controller and be directed over to it and if you need to do any renaming Resharper will find it...but it WON'T make any changes to the areas defined...so pick your battles.

Under most conditions, I can avoid using redirects entirely...but sometimes you gotta do what you gotta do.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!