1

From this answer on another thread, I've created a redirect controller to route my old aspx files to my new MVC pages.

RouteConfig.cs:

routes.MapRoute(
    name: "About",
    url: "aboutus.aspx",
    defaults: new { controller = "Redirect", action = "About" }
);

RedirectController.cs:

public ActionResult About()
{
    return RedirectPermanent("/Home/About");
}

This works perfectly, as I have a view in /Views/Home/About.cshtml. However, if I try to redirect, say a .PDF file, it doesn't work...

RouteConfig.cs:

routes.MapRoute(
    name: "MyPDF",
    url: "somefile.pdf",
    defaults: new { controller = "Redirect", action = "MyPDF" }
);

RedirectController.cs:

public ActionResult MyPDF()
{
    return RedirectPermanent("/Documents/somefile.pdf");
}

This goes to a standard 404 not found page. I've also tried an absolute path with no luck:

RedirectController.cs:

public ActionResult MyPDF()
{
    return RedirectPermanent("http://mydomain.com/Documents/somefile.pdf");
}

But still no luck. Any ideas?

edit: the reason for the redirect is because 3rd party sites are linking to old PDF files (they used to exist on the website root, now they're in /Documents/. I'd like to 301 redirect to the correct file.

Community
  • 1
  • 1
dmathisen
  • 2,269
  • 3
  • 36
  • 63
  • 2
    That's because ASP.MVC doesn't understand URLs with periods in them. By default, IIS gets control of those requests and tries to find the actual file. I'm not sure how to fix it though. – Alex Dresko Jan 27 '14 at 21:16

5 Answers5

0

why would you like to redirect to file? can you not just give a url to that file to a user in a view?

In case you insist on redirecting, just go:

return Redirect("~/Documents/myPdf.pdf");

But make sure there is Documents folder with myPdf.pdf file there. And your must not have Documents controller, otherwise it will not work. Or you'll need to work around it with adding another route to route table.

trailmax
  • 34,305
  • 22
  • 140
  • 234
  • One reason would be to log a static file download (at the cost of an extra HTTP request for redirection). – Simon Belanger Jan 27 '14 at 21:41
  • Never through of that! Thanks, Simon – trailmax Jan 27 '14 at 21:42
  • The reason I'm looking to redirect is because 3rd party sites are linked to the old PDF files. So I'd like to 301 redirect to the correct file. I tried your suggesting of 'return Redirect("~/Documents/myPdf.pdf");' but no luck still. – dmathisen Jan 27 '14 at 21:54
  • Sounds like you just need a rewrite-rule in IIS http://www.iis.net/learn/extensions/url-rewrite-module/creating-rewrite-rules-for-the-url-rewrite-module – trailmax Jan 27 '14 at 22:40
0

You should add the following to your routing configuration:

routes.IgnoreRoute("{*pdfExtension}", new {pdfExtension=@".*\.pdf(/.*)?"});

In case you would like to ignore PDF file extensions and be able to get redirected to a normal action when such an extension is used in the URL.

Paweł Bejger
  • 6,176
  • 21
  • 26
0

If you just want to return the document would it not make more sense to just return the file using the File() method on the controller.

It wouldn't require a redirect but just for you to have a physical file location.

Sorry if I misunderstood the question and the formatting of the answer it's from my phone.

here is the link

Qpirate
  • 2,078
  • 1
  • 29
  • 41
  • Thank you. I have to run but will take a look at the File() method tomorrow am. To clarify, I'd like to redirect because 3rd party sites are linked to old and incorrect PDF paths. – dmathisen Jan 27 '14 at 22:51
  • if the PDF files are stored on your local webserver and you have access to them then the File actionresult will work for you. i have added the link in the answer – Qpirate Jan 28 '14 at 16:44
0
routes.MapRoute(
    name: "MyPDF",
    url: "somefile.pdf",
    defaults: new { controller = "Redirect", action = "MyPDF" }
);

public ActionResult MyPdf()
        {
            return Redirect("http://www.myvlkbenefits.com/MedicalSummary.pdf");
        }

http://localhost/somefile.pdf -> redirected to -> that above

nothing is wrong with your code, maybe your url is not the same to what you type on the browser, thus giving you 404

sharif y
  • 513
  • 4
  • 11
0

For redirecting PDFs, I recommend using rewrite rules in Web.config instead of MVC controllers and RouteConfig.cs. It can look something like this:

Within system.webServer in Web.config

<rewrite>
  <rules>
    <rule name="Permanent Redirect Rule" stopProcessing="true">
      <match url=".*" />
      <conditions>
        <add input="{PermanentRedirects:{REQUEST_URI}}" pattern="(.+)" />
      </conditions>
      <action type="Redirect" url="{C:1}" appendQueryString="false" redirectType="Permanent" />
    </rule>
  </rules>
  <rewriteMaps>
    <rewriteMap name="PermanentRedirects">
      <add key="/somefile.pdf" value="/Documents/newfile.pdf"/>
    </rewriteMap>
  </rewriteMaps>
</rewrite>

Add a add line in the rewriteMap section for each PDF to redirect.