I have an action who displays a PDF in a new browser tab.
public ActionResult Print()
{
var cd = new ContentDisposition
{
FileName ="something.pdf",
Inline = true
};
Response.AppendHeader("Content-Disposition", cd.ToString());
return File(reportResponse.Data.Document, MediaTypeNames.Application.Pdf);
}
The filename is working fine. When I download the file it has the name I want "something.pdf".
The problem is when google chrome opens the PDF in a new browser tab, it displays the controller action name (Print) as the title of the PDF. That's what I'm trying to change. I attached a picture for clarification.
View code: Url.Action("Print", "Controller", new { area = "Area" }), new { @target = "_blank" }
C programming language is a machine-independent programming language that is mainly used to create many types of applications and operating systems such as Windows, and other complicated programs such as the Oracle database, Git, Python interpreter, and games and is considered a programming foundation in the process of ...
Compared to other languages—like Java, PHP, or C#—C is a relatively simple language to learn for anyone just starting to learn computer programming because of its limited number of keywords.
What is C? C is a general-purpose programming language created by Dennis Ritchie at the Bell Laboratories in 1972. It is a very popular language, despite being old. C is strongly associated with UNIX, as it was developed to write the UNIX operating system.
Full form of C is “COMPILE”. One thing which was missing in C language was further added to C++ that is 'the concept of CLASSES'.
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.
.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With