If you want to "tell the browser" that an error has occurred, the standard "HTTP method" is to return a 500 status code, especially if your request is called using Ajax so that you can gracefully handle the exception.
I would suggest just throwing an Exception if no report was found for the provided id :
public FileResult GetReport(string id) { // could internally throw the Exception inside 'GetReport' method byte[] fileBytes = _manager.GetReport(id); // or... if (fileBytes == null || !fileBytes.Any()) throw new Exception(String.Format("No report found with id {0}", id)); return File(fileBytes, MediaTypeNames.Application.Octet, fileName = id+ ".pdf"); }
Explicitly redirecting to an error page or returning a ViewResult not the best approach in ASP.NET MVC, as this is usually the role of the HandleError filter (which is used by default), which can be easily configured to either redirect or render some kind with exception details (when saving HTTP status 500).
This is all true if one assumes that refusal to receive a report is indeed considered an exception. If this is not the case (for example, if we expect that some report will not have an available file for dump), explicit return of the Redirect/View result is quite acceptable.
source share