ActionResult return to page that called it

asp.net-mvc

Solution

There are a couple of tricks that you can use for this.

The simplest is ...

return Redirect(HttpContext.Request.UrlReferrer.AbsoluteUri);

AbsoluteUri may not give you the exact path you are looking for, but UrlReferrer should have the imformation you are looking for. Redirect returns a subclass of ActionResult so it is a valid return value.

Another idea is to base the redirect location off of stored values. This is useful when you are going to make multiple requests before you want to redirect, such as when you validate a form and show validation issues on the first response. Another situation will be when the referrer is not a local site. In either case, your referrer won't be what you want it to and you will need to retrieve the correct location from somewhere else.

Specific implementations include using a hidden input field on your form, session state, pulling a descriminator value from your route data, or even just a more constant value like HttpContext.Request.ApplicationPath.

Good luck.

Problem

I have a ActionLink, that calls my public ActionResult, and I would like it to return back to the page that it was called from, but how?

Original source