C# MVC: Chrome using the action name to set inline PDF title C# MVC: Chrome using the action name to set inline PDF title google-chrome google-chrome

C# MVC: Chrome using the action name to set inline PDF title


To the action method pass parameter of the file name and make sure parameter name is 'id'.

View code:

Url.Action("Print", "Controller", new { id = "filename", area = "Area" }), new { @target = "_blank" }


Adding to kodandarami's answer,

When you're passing a PDF file back from ASP.NET MVC, the page title is set to the last url part of the route config used to get to your controller.

Knowing this, you can dynamically name the title by setting up a parameter as part of the route config for your controller eg:

        routes.MapRoute(            "Print",            "print/{pdfName}",            new { controller = "Print", action = "Index", pdfName = UrlParameter.Optional }        );

Then MVC will use this value instead as it considers it a separate page.

Use it in the url as"/print/this-is-my-pdf-title" not '/print?pdfName=this-is-my-pdf-title".

As a querystring parameter, MVC will just fall back to calling the PDF 'print'.

Note:As mentioned in this related question,

IIS has more strict rules about forbidden characters when the text is before the query string '?' character. You are not allowed <,>,*,%,&,:,\ (See MS Reference)

Any of these characters, even when encoded will give you a 'Path is potentially dangerous' 400 error.

Make sure to remove/replace these forbidden characters.

.


I solved this problem by using a iframe.

Create an action which fills the title and pdf url.

        public ActionResult ArticleSpecification(int ArticleID)        {            using (var context = new myEntities())            {                Article article = context.Article.FirstOrDefault(a => a.ArticleID == ArticleID);                ViewData["Title"] = article.Code + " - " + article.Description;                ViewData["PdfSource"] = "ArticleSpecificationPdf?ArticleID=" + article.ArticleID;                return View("~/Views/Specification/pdfview.cshtml");            }        }

pdfview.cshtml: with a iframe to view the pdf and title.

@{    Layout = "";}<!DOCTYPE html><html>    <head>        <title>@ViewData["Title"]</title>        <style>            html, body, iframe {                height: 100%;                width: 100%;                margin: 0;                border: 0;            }            body {                overflow-y: hidden;            }        </style>    </head>    <body>        <iframe src="@ViewData["PdfSource"]"></iframe>    </body></html>

The action to return the pdf content.

        public FileResult ArticleSpecificationPdf(int ArticleID)        {            using (var context = new myEntities())            {                PdfFile file = null;                Article article = context.Article.FirstOrDefault(a => a.ArticleID == ArticleID);                if (article != null && article.SpecificationPdfID != null)                    file = context.PdfFile.FirstOrDefault(a => a.PdfFileID == article.SpecificationPdfID);                return new FilePathResult(file.path, "application/pdf");            }        }