-->

RedirectToAction between areas?

2019-01-30 04:54发布

问题:

Is there a way to redirect to a specific action/controller on a different Area?

回答1:

Did you try this?:

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


回答2:

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 = "" });


回答3:

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.