Asp.net mvc 2.0: requesting a route to a static resource

I want to redirect HTTP requests not to an action, but to a file.

Important: I have a working solution using the IIS 7.0 URL rewriting module, but for debugging at home (without IIS 7.0) I can not use URL rewriting.

Specific situation
I want to specify any URL containing /images/in a folder ~/images/.

Example:

http://wowreforge.com/images/a.png -> /images/a.png
http://wowreforge.com/Emerald Dream/Jizi/images/a.png -> /images/a.png
http://wowreforge.com/US/Emerald Dream/Jizi/images/a.png -> /images/a.png
http://wowreforge.com/characters/view/images/a.png -> /images/a.png

The problem is that the page "view_character.aspx" can be obtained from several URLs:

http://wowreforge.com/?name=jizi&reaml=Emerald Dream
http://wowreforge.com/US/Emerald Dream/Jizi

Context IIS 7.0 (integrated mode), ASP.NET MVC 2.0

Additional credit issues

  • Good idea to use ASP.NET MVC routing in this situation instead of rewriting the URL?
  • IIS 7.0 ?
+3
1

, .

<img src="<%= ResolveUrl("~/images/a.png") %>" />

, , .

UPDATE RouteTable

routes.Add("images", new Route("{*path}", null, 
   new RouteValueDictionary(new { path = ".*/images/.*"}), 
   new ImageRouteHandler()));

ImageRouteHandler ImageHandler

public class ImageRouteHandler : IRouteHandler
{
    public IHttpHandler GetHttpHandler(RequestContext requestContext)
    {
        //you'll need to figure out how to get the physical path
        return new ImageHandler(/* get physical path */);
    }
}

public class ImageHandler : IHttpHandler
{
    public string PhysicalPath { get; set; }

    public ImageHandler(string physicalPath)
    {
        PhysicalPath = physicalPath;
    }

    public void ProcessRequest(HttpContext context)
    {
        context.Response.TransmitFile(PhysicalPath);
    }

    public bool IsReusable
    {
        get { return true; }
    }
}

. System.Web.StaticFileHandler Reflector , Asp.Net .

+3

Source: https://habr.com/ru/post/1785971/


All Articles