In this article I will explain with an example, how to display (show) PDF file embedded in View in ASP.Net MVC Razor.
This article will explain how to view PDF files within browser without downloading them in ASP.Net MVC Razor.
 
 
Controller
The Controller consists of two Action methods.
Action method for handling GET operation
Inside this Action method, simply the View is returned.
 
Action method for handling POST operation
This Action method gets called when the Form is submitted due to the click of the View button. The PDF will be embedded and viewed in browser using HTML OBJECT tag.
The HTML OBJECT tag is generated into an HTML string consisting of the URL of the PDF file.
The generated HTML string is set into a TempData object and finally redirected to Index action.
Note: For more details on TempData object, please refer my article ASP.Net MVC: TempData Tutorial with example.
 
public class HomeController : Controller
{
    // GET: Home
    public ActionResult Index()
    {
        return View();
    }
 
    [HttpPost]
    public ActionResult ViewPDF()
    {
        string embed = "<object data=\"{0}\" type=\"application/pdf\" width=\"500px\" height=\"300px\">";
        embed += "If you are unable to view file, you can download from <a href = \"{0}\">here</a>";
        embed += " or download <a target = \"_blank\" href = \"http://get.adobe.com/reader/\">Adobe PDF Reader</a> to view the file.";
        embed += "</object>";
        TempData["Embed"] = string.Format(embed, VirtualPathUtility.ToAbsolute("~/Files/Mudassar_Khan.pdf"));
 
        return RedirectToAction("Index");
    }
}
 
 

View
The following View consists of an HTML Form which posts to the ViewPDF action method discussed earlier. The Form consists of an HTML Anchor Hyperlink which when clicked submits the Form.
The TempData object is displayed using the Raw HTML Helper method which is used for rendering HTML content.
@{
    Layout = null;
}
 
<!DOCTYPE html>
 
<html>
<head>
    <meta name="viewport" content="width=device-width"/>
    <title>Index</title>
    <style type="text/css">
        body {
            font-family: Arial;
            font-size: 10pt;
        }
    </style>
</head>
<body>
    @using (Html.BeginForm("ViewPDF", "Home", FormMethod.Post))
    {
        <a href="javascript:;" onclick="document.forms[0].submit();">View PDF</a>
        <hr/>
        @Html.Raw(TempData["Embed"])
    }
</body>
</html>
 
 
Screenshots
Adobe PDF Reader installed
Display (Show) PDF file embedded in View in ASP.Net MVC Razor
 
Adobe PDF Reader not installed
Display (Show) PDF file embedded in View in ASP.Net MVC Razor
 
 
Downloads