3

today i came up with a requirement for my company website (build in ASP.NET MVC 3).

One of the static pages, a pdf file (from Content folder) from my companies website is shown up in Google searches. My company wants that pdf file to be accessible to only logged in users.

For that 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 action method OpenContentResources which would redirect the user to the URL

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

But this code goes in infinite loop and never gets executed. Can any one help me around with my concern.

Thanks ...

4

3 に答える 3

2

私はこのようにします:

コントローラ:

    [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) あなたの場合は異なる場合があります。

参考文献:

ロボット.txt

web.config ロケーション要素

于 2012-05-18T12:39:55.377 に答える
1

You have to return the pdf file as a FileResult. See this post for more information

ASP.NET MVC - How do I code for PDF downloads?

In your case the action will looks like

public ActionResult OpenContentResources()
{
    return File("~/Content/PDF/ABC.pdf", "application/pdf");
}
于 2012-05-18T10:19:38.763 に答える
0

テストサーバーでホストした後、問題は解決しました。

于 2012-05-22T11:10:31.523 に答える