user3909934
user3909934

Reputation: 25

How To Send Value in HttpContext.Current.Response.Redirect?

I want to send an error message in HttpContext.Current.Response.Redirect

How should I do it?

HttpContext.Current.Response.Redirect("/Home/ErrorPage("the error is from here")");       

public ActionResult ErrorPage(string error=")
{
     return View(error);
}

How should I show the error inside the view?

Upvotes: 1

Views: 2395

Answers (2)

StuartLC
StuartLC

Reputation: 107267

Your URL is incorrect. You would need a result which has a valid Url encoded query string:

"/Home/ErrorPage?error=the+error+is+from+here";

However, instead of direct construction, you should use Html helper methods to build the url, e.g.:

Url.Action("ErrorPage", "Home", new {error = "the error is from here"});

You can also use TempData to pass once-off information:

Note that as per @Vsevolod's comment, that you shouldn't use Response.Redirect directly. Use an MVC RedirectResult or RedirectToAction from your controller, e.g.:

public ActionResult MethodReportingError()
{
  TempData["Error"] = "Bad things happened";
  return new RedirectResult(Url.Action("ErrorPage", "Home"));
}

public ActionResult ErrorPage()
{
    return View(TempData["Error"]);
}

Upvotes: 1

Anirudha Gupta
Anirudha Gupta

Reputation: 9289

Where you want to pass Error. You are calling the information in url that is totally incorrect.

You can better pass information to error instead of url so it will make small and better url instead of blah blah in url.

Try to use Httpcontext.Controllers.ViewData to pass error to view and render the error there.

Upvotes: 0

Related Questions