首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >RedirectToAction不工作

RedirectToAction不工作
EN

Stack Overflow用户
提问于 2012-02-01 05:07:14
回答 3查看 977关注 0票数 1

RedirectToAction不显示视图。

代码语言:javascript
复制
    // Go populate and display PDF using XML file
    DoPDF(stXML); 
}
UpDateDropDown(model);
return RedirectToAction("ReportsSelection", "Reports");

渲染代码:

代码语言:javascript
复制
private void DoPDF(String stXML)
{
    string filename = string.Concat(Guid.NewGuid().ToString(), ".pdf");
    PdfReader reader = new PdfReader(new RandomAccessFileOrArray(Request.MapPath(_NFCPage._NFReference.FM_NOFEAR_PDF)), null);
    // Create the iTextSharp document
    // Set the document to write to memory

    using (MemoryStream memStream = new MemoryStream())
    {
        PdfStamper ps = new PdfStamper(reader, memStream);
        // Populate the PDF with values in the XML file
        AcroFields af = ps.AcroFields;
        ParserXML(stXML, af);
        ps.FormFlattening = false;
        ps.Writer.CloseStream = false;
        ps.Close();
        byte[] buf = new byte[memStream.Position];
        memStream.Position = 0;
        memStream.Read(buf, 0, buf.Length);
        // Set the appropriate ContentType
        Response.ContentType = "Application/pdf";
        // Get the physical path to the file
        Response.AddHeader("Content-disposition", string.Format("attachment; filename={0};", filename));
        // Write the file directly to the HTTP content output stream.
        Response.Buffer = true;
        Response.Clear();

        Response.BinaryWrite(memStream.GetBuffer());  //Comment out to work
        Response.End();                               //Comment out to work
    }
}

我注意到,如果删除DoPDF例程中的最后两行,它会显示视图。

EN

回答 3

Stack Overflow用户

发布于 2012-02-01 05:12:11

Response.End()将使服务器发送HTTP响应。此时,您的浏览器将认为请求已完成,不会发生重定向。你能提供更多关于你正在努力完成的事情的上下文吗?然后我们可以更好地了解如何帮助您。

票数 3
EN

Stack Overflow用户

发布于 2012-02-01 05:10:44

编辑: nvm,你在那里有一个Response.End()调用,这将结束请求的执行,你的重定向显然不会起作用。如果你正在尝试刷新流,那么你需要改为使用Response.Flush()

票数 1
EN

Stack Overflow用户

发布于 2012-02-01 05:17:45

不要在MVC中处理文件下载,正如你所看到的,这可能会导致问题……

代码语言:javascript
复制
return File(memStream, "Application/pdf", filename);

会为你做所有的事。

MSDN

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/9086967

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档