如何从asp.net处理程序返回404错误? [英] How can I return a 404 error from an asp.net handler?

查看:99
本文介绍了如何从asp.net处理程序返回404错误?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经创建了一个用于下载文件的处理程序。
如果文件不存在或用户无权下载该特定文件,我想返回404错误。

I have created a handler for downloading a file. I want to return a 404 error if the file does not exists or user does not have rights to download that particular file.

可以吗?如果是,怎么样?

Is it possible? If yes, how? Sample code would be appreciated.

推荐答案

我不知道我们有足够的信息,你想要做什么。你在使用REST API吗?如果您使用的是WebApi,那将很简单:

I'm not sure we have enough information about what you're trying to do. Are you using a REST API? If you're using WebApi, it'd be pretty simple:

public IHttpActionResult DownloadFile(string fileName)
{
   if (!File.Exists(fileName))
   {
      return NotFound();
   }

   // Do something

   return Ok(yourFile);
}

这篇关于如何从asp.net处理程序返回404错误?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆