I have a controller action that downloads a file from an azure blob based on the container reference name (i.e. full path name of the file in the blob). The code looks somet
The correct way to handle a not found in a web application is by returning a 404 HTTP status code to the client which in ASP.NET MVC terms translates into returning a HttpNotFoundResult from your controller action:
return new HttpNotFoundResult();
Ahh, oops, didn't notice you were still on ASP.NET MVC 2. You could implement it yourself because HttpNotFoundResult
was introduced only in ASP.NET MVC 3:
public class HttpNotFoundResult : ActionResult
{
public override void ExecuteResult(ControllerContext context)
{
if (context == null)
{
throw new ArgumentNullException("context");
}
context.HttpContext.Response.StatusCode = 404;
}
}
NotFound()
Your controller must inherit of Controller
and the method must return ActionResult
Example:
public ActionResult GetFile(string path)
{
if (!File.Exists(path))
{
return NotFound();
}
return new FileContentResult(File.ReadAllBytes(path), "application/octet-stream");
}