如何在HttpContext.Current.Response.Redirect中发送值?

时间:2014-12-25 12:35:42

标签: c# asp.net-mvc asp.net-mvc-5

我想在HttpContext.Current.Response.Redirect中发送错误消息

我该怎么做?

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

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

我应该如何在视图中显示错误?

2 个答案:

答案 0 :(得分:0)

您要传递错误的位置。您正在调用url中完全不正确的信息。

你可以更好地将信息传递给错误而不是网址,这样它就会在网址中制作小而好的网址而不是等等。

尝试使用Httpcontext.Controllers.ViewData传递错误以查看并在那里呈现错误。

答案 1 :(得分:0)

您的网址不正确。您需要一个具有有效Url编码查询字符串的结果:

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

但是,您应该使用Html helper methods来构建网址,而不是直接构建,例如:

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

您还可以使用TempData传递一次性信息:

请注意,根据@ Vsevolod的评论,您不应直接使用Response.Redirect。使用控制器中的MVC RedirectResultRedirectToAction,例如:

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

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