Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ActionResult return to page that called it

Tags:

asp.net-mvc

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?

like image 685
Coppermill Avatar asked Aug 17 '09 15:08

Coppermill


People also ask

Can I return ActionResult Instead of view results?

First to Understand---ActionResult and ViewResult are basically a return type for an method(i.e Action in MVC). Second The Difference is --- ActionResult can return any type of Result Whereas ViewResult can return result type of View Only.

Can I return ActionResult?

The ActionResult types represent various HTTP status codes. Any non-abstract class deriving from ActionResult qualifies as a valid return type.

What can I return from ActionResult in MVC?

An ActionResult is a return type of a controller method in MVC. Action methods help us to return models to views, file streams, and also redirect to another controller's Action method.

Can we return string from ActionResult?

You can't return a string from a method which returns an ActionResult, so in this case you return Content("") as swilliams explained. If you only ever need to return a string, then you would have the method return a string, as Phil explained.


1 Answers

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.

like image 169
Chuck Avatar answered Nov 15 '22 21:11

Chuck