Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I return a 404 error from an asp.net handler?

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.

like image 718
Bilal Fazlani Avatar asked Dec 31 '12 05:12

Bilal Fazlani


1 Answers

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);
}
like image 52
Ari Roth Avatar answered Sep 28 '22 07:09

Ari Roth