Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I raise an http 404 from a FileStreamResult?

Normally I'd return HttpNotFound() but my method returns a FileStreamResult and I get

Cannot implicitly convert type 'System.Web.Mvc.HttpNotFoundResult' to 'System.Web.Mvc.FileStreamResult'

When I try to build. Is there an alternative version for file results?

like image 631
mpen Avatar asked Sep 08 '12 02:09

mpen


People also ask

How to handle 404 error in web api. net core?

To do this, type http://localhost:6440/welcome in the address bar of your browser while the application is in execution. When the ASP.NET Core MVC engine fails to locate the resource for the specified URL, a 404 error will be returned and you will be presented with the following error page.

What action result helper method on a controller will return an HTTP status code 404?

How to return 404 status code from a MVC application. First way is to instantiate HttpNotFoundResult class and return the object. Next alternative is to makes use of HttpNotFound() helper method of the Controller class which returns the HttpNotFoundResult instance.


2 Answers

Type your action method to return the base ActionResult rather than the specific FileStreamResult. Then you can return whatever you want.

like image 134
Levi Avatar answered Oct 18 '22 17:10

Levi


You could throw an HttpException.

throw new HttpException(404, "Not found");
like image 29
McGarnagle Avatar answered Oct 18 '22 17:10

McGarnagle