开发者

How can I route "/img/" to the "/Content/img" folder in ASP.NET MVC 3?

开发者 https://www.devze.com 2023-03-17 02:41 出处:网络
I want all the urls for my images in my ASP.NET MVC application to be of the form: www.mydomain.com/img/so开发者_JAVA技巧meimagename.png

I want all the urls for my images in my ASP.NET MVC application to be of the form:

www.mydomain.com/img/so开发者_JAVA技巧meimagename.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?


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.


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>


what about this approach,

 public ActionResult Img(string imgURL)
    {
        ViewBag.imageURL = imgURL;
        return View();
    }

and the razor view

<img src="@Url.Content("~/Content/images/"+@ViewBag.imageURL)" />

or in a basic asp.net view

 <img src="~/Content/images/"+ViewBag.imageURL)" />
0

精彩评论

暂无评论...
验证码 换一张
取 消