C# 当出现拒绝访问错误时,停止响应写入文件

C# 当出现拒绝访问错误时,停止响应写入文件,c#,asp.net,C#,Asp.net,我有以下代码 try { GetFileAndDisplay() // Gets the file from the server and writes the file in a Response stream } catch (UnauthorizedAccessException ex) { Page.ClientScript.RegisterStartupScript(this.GetType(), "Msg", "<script type='tex

我有以下代码

try
{        
    GetFileAndDisplay() // Gets the file from the server and writes the file in a Response stream
}
catch (UnauthorizedAccessException ex)
{
    Page.ClientScript.RegisterStartupScript(this.GetType(), "Msg", "<script type='text/javascript'>alert('Error')</script>");
    Response.Clear();                
    Response.ContentType = "";               
    Response.End();                
}
试试看
{        
GetFileAndDisplay()//从服务器获取文件并将文件写入响应流中
}
捕获(未经授权的访问例外)
{
Page.ClientScript.RegisterStartupScript(this.GetType(),“Msg”,“警报('Error'));
Response.Clear();
Response.ContentType=“”;
Response.End();
}
尽管文件已损坏,但客户端仍会下载该文件,但是否有方法阻止下载该文件。

试试看
try
{        
    GetFileAndDisplay() // Gets the file from the server and writes the file in a Response stream
}
catch (UnauthorizedAccessException ex)
{
    Page.ClientScript.RegisterStartupScript(this.GetType(), "Msg", "<script type='text/javascript'>alert('Error')</script>");
    Response.Clear();                
    Response.Redirect("path/to/some/page/that/tells/the/user/something/is/wrong")
}
{ GetFileAndDisplay()//从服务器获取文件并将文件写入响应流中 } 捕获(未经授权的访问例外) { Page.ClientScript.RegisterStartupScript(this.GetType(),“Msg”,“警报('Error')); Response.Clear(); Response.Redirect(“path/to/some/page/that/tells/the/user/something/is/error”) }
当您捕获未经授权的访问异常时,为什么不抛出带有相关HTTP状态代码(即401)的HttpException-


你的代码也有问题吗?或者你想问一些关于它的问题吗?@Kristof不,代码没有问题。错误被捕获,但文件仍被下载下载下载了什么文件?你在哪里下载文件?例外情况是什么?另外,显示
GetFileAndDisplay()
代码。你不能在catch时删除文件吗?谢谢,但我不想重定向用户。那么你想为他做什么呢?我想我会保持简单并重定向他们。谢谢你的意见
throw new HttpException(401, ex.Message);