简体   繁体   English

如何从控制器操作重定向/返回aspx页面?

[英]How can I redirect/return an aspx page from a controller action?

How Can I return a particular aspx page wiht the path from a controller action? 如何通过控制器操作的路径返回特定的aspx页面?

Here is how I redirect from a controller action: 这是我从控制器操作重定向的方式:

Response.Redirect("_PDFLoader.aspx?Path=" + FilePath  +  id + ".pdf");

Even tried the following: 甚至尝试了以下方法:

return Redirect("_PDFLoader.aspx?Path=" + FilePath + id + ".pdf");

Here is my _PDFLOader.aspx page: 这是我的_PDFLOader.aspx页面:

<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="_PDFLoader.aspx.cs" Inherits="Proj._PDFLoader" %>

Here is my CodeBehind file: 这是我的CodeBehind文件:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.IO;

namespace Proj
{
    public partial class _PDFLoader : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            string OutfilePath = Request.QueryString["Path"].ToString();
            FileStream objfilestream = new FileStream(OutfilePath, FileMode.Open, FileAccess.Read);
            int len = (int)objfilestream.Length;
            Byte[] documentcontents = new Byte[len];
            objfilestream.Read(documentcontents, 0, len);
            objfilestream.Close();

            if (File.Exists(OutfilePath)) File.Delete(OutfilePath);       

            Response.ContentType = "application/pdf";
            Response.AddHeader("content-length", documentcontents.Length.ToString());
            Response.BinaryWrite(documentcontents);

        }
    }
}

Any help is much appreciated. 任何帮助深表感谢。

The following should work: 以下应该工作:

public class SomeController: Controller
{
    public ActionResult SomeAction() 
    {
        return Redirect("~/_PDFLoader.aspx?Path=" + Url.Encode(FilePath + id) + ".pdf"");
    }
}

But from what I can see all that this _PDFLoader.aspx WebFrom does is to serve the file and then delete it. 但是从我可以看到_PDFLoader.aspx WebFrom所做的全部工作是提供文件,然后将其删除。 You could do this directly from your controller action: 您可以直接通过控制器操作执行此操作:

public class SomeController: Controller
{
    public ActionResult SomeAction() 
    {
        string path = FilePath + id + ".pdf";
        if (!File.Exists(path))
        {
            return HttpNotFound();
        }
        byte[] pdf = System.IO.File.ReadAllBytes(path);
        System.IO.File.Delete(path);
        return File(pdf, "application/pdf", Path.GetFileName(path));
    }
}

and if you want the file to be displayed inline instead of downloading it: 并且如果您希望文件以内联方式显示而不是下载:

return File(pdf, "application/pdf");

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM