MVC 3: Routing to Static Pages

Today I have a requirement for my company website (in ASP.NET MVC 3).

One of the static pages - a PDF file (from the "Content" folder) from the website of my companies is displayed in Google search results. My company wants the pdf file to be available only for login.

To do this, I created a route and decorated it with RouteExistingFiles = true;

        routes.RouteExistingFiles = true;
        routes.MapRoute(
            "RouteForContentFolder", // Route name
            "Content/PDF/ABC.pdf", // URL with parameters
            new { controller = "User", action = "OpenContentResources", id = UrlParameter.Optional } // Parameter defaults
        );

In UserController, I wrote an OpenContentResources action method that redirects the user to a URL

    [CompanyAuthorize(AppFunction.AccessToPDFFiles)]
    public ActionResult OpenContentResources()
    {
        return Redirect("http://localhost:9000/Content/PDF/ABC.pdf");
    }

But this code runs in an infinite loop and never runs. Can anyone help me with my care.

Thank...

+3
source share
3 answers

I would do it like this:

Controller:

    [Authorize]
    public ActionResult GetPdf(string name)
    {
        var path = Server.MapPath("~/Content/Private/" + name);
        bool exists = System.IO.File.Exists(path);
        if (exists)
        {
            return File(path, "application/pdf");
        }
        else
        {
            // If file not found redirect to inform user for example
            return RedirectToAction("FileNotFound");
        }
    }

web.config:

  <location path="Content/Private" allowOverride="false">
    <system.web>
      <authorization>
        <deny users="*"/>
      </authorization>
    </system.web>
  </location>

robots.txt( ):

User-agent: *
Disallow: /Content/Private/

, , . , , . (http://localhost:8080/Home/GetPdf?name=test.pdf). .

:

Robots.txt

web.config

+2

PDF FileResult. .

ASP.NET MVC - PDF?

public ActionResult OpenContentResources()
{
    return File("~/Content/PDF/ABC.pdf", "application/pdf");
}
+1

.

0

All Articles