Reputation: 53
I want all the urls for my images in my ASP.NET MVC application to be of the form:
www.mydomain.com/img/someimagename.png
However, I currently have all my images in my MVC project in the: /Content/img/ folder.
How can I write a route that will map /img to /Content/img?
Upvotes: 5
Views: 1498
Reputation: 822
You can use rewrite rules for this. Add following route to rewrite section in web.config:
<rule name="Img to content img">
<match url="^img/(.*)" />
<action type="Rewrite" url="/Content/img/{R:1}" />
</rule>
Upvotes: 4
Reputation: 97
what about this approach,
public ActionResult Img(string imgURL)
{
ViewBag.imageURL = imgURL;
return View();
}
and the razor view
<img src="@Url.Content("~/Content/images/"[email protected])" />
or in a basic asp.net view
<img src="~/Content/images/"+ViewBag.imageURL)" />
Upvotes: 2
Reputation: 9173
Images don't pass through the ASP.NET stack. IIS handles those requests. You need to write an HttpModule if you want your ASP.NET application to handle image requests. This blog shows you how to do this: http://weblogs.asp.net/jgaylord/archive/2009/05/04/letting-asp-net-handle-image-file-extensions-in-iis-6.aspx
But an easier solution would be to just create a virtual directory in IIS.
Upvotes: 5