Delmonte
Delmonte

Reputation: 411

MVC web site: localhost page can’t be found http error 404

I'm developing a Asp.net MVC Core 2.2 web site in IIS. The IIS website that hosts my MVC Core project has been created correctly. My project generates correctly a PDF file. Usually, I just use a jquery function to open that PDF file in browser:

 $.ajax({
            url: '@Url.Action("CreatePDF", "MyApiController", new { area= "MyArea" })',            
            dataType: 'json',                
            contentType: "application/json",
            type: 'POST',
            cache: false,            
            data: JSON.stringify(Filtro),
            success: function (d) {

                if (d.success == true) {
                    var item = d.data;
                    var pageurl = '@Url.Content("~/")' + 'Temporal/' + item.PDFName;
                    window.open(pageurl, '_blank');

                }

            },

However, I just realized that this MVC Core web site doesn't allow to open any PDF or txt or jpg or whatever file in browser and instead output this message:

localhost page can’t be found 
http error 404 

My question is:

In a MVC Core web site, how should I open a PDF file in browser from Controller

        [Route("[action]")]
        [HttpPost]
        public ActionResult<BE_Reporte> CreatePDF(BE_Reporte c)
        {

            BL_Reporte b = new BL_Reporte();

            try
            {
                c = b.CreatePDF(c);

                return Ok(new
                {
                    data = c,
                    success = true,
                    Message = "PDF created OK"

                });

            }
            catch (Exception ex) { throw ex; }
            finally
            {
                b.Dispose(); b = null;
            }

        }

Upvotes: 0

Views: 1619

Answers (2)

Edward
Edward

Reputation: 30056

The generated Url for '@Url.Content("~/")' + 'Temporal/' + item.PDFName; will be something like /Temporal/Test.pdf. For this request url, you need to place the Test.pdf in the place CoreMVC2_2\wwwroot\Temporal\Test.pdf.

If you place the Temporal in other folder except wwwroot, it is recommended to move it to wwwroot, otherwise, you need to configure the Serve files outside of web root.

And for client side, you may need to try code below:

var pageurl = '@Url.Content("~/")' + 'Temporal/' + 'Test.pdf';
console.log(pageurl);
var newWindow = window.open("", '_blank');
newWindow.location = pageurl;

Upvotes: 1

WantaBeCoder
WantaBeCoder

Reputation: 1

Hey the 404 Error says the server did not find the resource you requested. First try to place a break-point at the beginning of the controller action CreatePDF. Then change your attribute routing to [HttpPost("[action]")] and remove the leading [Route("[action]")] call as it might be causing a routing error.
Also, try and add [FromBody] before your parameter.

...([FromBody] BE_Reporte ...)

Also you can use <iframe> HTML tag to display a pdf file or use <embed> as noted here: Load Generated PDF Data into IFRAME on ASP.NET MVC

Upvotes: 0

Related Questions