Fantasterei
Fantasterei

Reputation: 462

ASP.NET 4 - url routing for images

i'm trying to route files like images from subfolder to root but don't have any idea to do this. Also i've checked some scripts around the web, but nothing of these would work.

Source: /Projects/test/images/img.jpg
Destination: /images/img.jpg

I would be thankful for help.

Upvotes: 0

Views: 698

Answers (2)

Lasse Edsvik
Lasse Edsvik

Reputation: 9298

Try something like this:

Route:

routes.MapRoute("Image", "Images/{file}",
                 new { controller = "Images", action = "Images" }
);

Controller:

public ActionResult Images(string file)
{
    path = "/Projects/test/images/" + file;
    if (!System.IO.File.Exists(path))
    {
        return new HttpNotFoundResult();
    }

    return File(path, "image/jpeg");
}

Upvotes: 2

Robert McKee
Robert McKee

Reputation: 21477

Use URL Rewrite rules for this.

Upvotes: 1

Related Questions