ASP.NET MVC 无法在 HTTP 标头之后重定向



我在 IIS 中收到此警告,它是否与从浏览器请求 cookie 或我如何使用Response.Redirect有关?从一些研究中,我看到我需要添加的建议Response.Buffer = true;这是正确的吗?

法典

public ActionResult GetTemplateById(string TemplateId)
{    
string loginUrl = ConfigurationManager.AppSettings["loginUrlCottTemplateId"];
Request.Cookies.Add(new HttpCookie("ev", "sJlp+HNulad4GaX2RCjjlQ=="));
if (Request.Cookies["customerguid"] != null)
{
HttpCookie cookie = Request.Cookies["customerguid"];
string cookieValue = cookie.Value;
cookieValue = cookieValue.Replace("%2D", "-");
}
else
{
Response.Redirect(loginUrl + TemplateId);
}
if (!AuthenticationRep.IsUserValid(Request))
{
return Redirect(storeProductLink);
}
var memoryStream = new System.IO.MemoryStream(fm.FileData);
return new FileStreamResult(memoryStream, "application/pdf");
}

IIS 异常

异常消息:发送 HTTP 标头后无法重定向。

线程信息:线程ID:213
线程帐户名:
IIS APPPOOL.NET v4.5
正在模拟:假

堆栈跟踪:

at System.Web.HttpResponse.Redirect(String url, Boolean endResponse, Boolean permanent(

在 ASP.NET MVC 应用程序中,您使用Response.Redirect(someurl)重定向。
在 ASP.NET Web 窗体应用程序中使用时,永远不会执行Response.Redirect(someurl)之后的任何代码。但是,当您在 MVC 中使用相同的 ASP.NET 代码执行即使在发送重定向响应后,代码执行也会继续。要知道为什么,请经历这个。

您的代码正在尝试在同一操作结果中重定向两次,通过 -

Response.Redirect(loginUrl + TemplateId); 

return Redirect(storeProductLink);    

我相信这是抛出这个错误。

请将Response.Redirect(loginUrl + TemplateId)更改为return Redirect(loginUrl + TemplateId),并检查这是否可以解决您的问题。

最新更新