Search code examples
asp.net-mvcvisual-studiopdffilenotfoundexceptionserver-error

How to view a file that's part of an MVC project?


I have a file, eulatest.pdf, that I want to be able to view through my website. At this point I don't really care whether it's at mywebsite.com/eulatest.pdf or mywebsite.com/eula/eulatest.pdf or any variation; I can't get any of these to work. How can I view this file? I'm not even trying to access it through code; I just can't figure out where it is or how to access it at all.

I keep getting this error:

The resource cannot be found

And I've tried putting the file in all of these locations:

Solution Explorer


Solution

  • You need to add an action in one of your controllers to stream out that file.

    public class EULAController : Controller
    {
        public ActionResult Index()
        {
            return File("eulatest.pdf","application/pdf");
        }
    }
    

    Assuming the file is in the root folder of your application. For more on File checkout this

    If this is in EULA Controller use:

     mywebsite.com/eula/
    

    Alternatively you can add that file to ignore lists in your route mapping.