ASP.NET MVC 4 FileResult - In Error

I have a simple action on a controller that returns a PDF.

It works great.

public FileResult GetReport(string id) { byte[] fileBytes = _manager.GetReport(id); string fileName = id+ ".pdf"; return File(fileBytes, MediaTypeNames.Application.Octet, fileName); } 

When the manager does not receive the report, I return null or empty byte[] .

How can I contact the browser that there is a problem when the result is set to FileResult ?

+6
source share
4 answers

I would change the return type of your method to ActionResult.

 public ActionResult GetReport(string id) { byte[] fileBytes = _manager.GetReport(id); if (fileBytes != null && fileBytes.Any()){ string fileName = id+ ".pdf"; return File(fileBytes, MediaTypeNames.Application.Octet, fileName); } else { //do whatever you want here return RedirectToAction("GetReportError"); } } 
+14
source

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.

+7
source

The FileResult class inherits from ActionResult . So, you can define your action as follows:

 public ActionResult GetReport(string id) { byte[] fileBytes = _manager.GetReport(id); string fileName = id + ".pdf"; if(fileBytes == null || fileBytes.Length == 0) return View("Error"); return File(fileBytes, MediaTypeNames.Application.Octet, fileName); } 
+6
source

Another workaround for handling preconditions is to split the boot process into two steps. First of all, you need to check the prerequisites in the server-side method, which runs as the ajax / post method.

Then, if these prerequisites are met, you can start downloading the request (for example, in the onSuccess callback, where it checks for a return value indicating execution), in which (on the server side) you will handle potential exceptions in the way the messages described above.

+1
source

Source: https://habr.com/ru/post/959187/


All Articles