UrlRewrite for photo name - Web

2

Today have the urls of the images of my site as:

/images/tb/1280077_894mvzfxoojqb.jpg

I would like to be able to rename this so that the generated HTML is

Apartamento_em_SaoPaulo.jpg or something similar.

Possible options.

Apartamento_em_SaoPaulo__1280077_894mvzfxoojqb.jpg

or

/1280077_894mvzfxoojqb/Apartamento_em_SaoPaulo.jpg

This is for SEO reasons. That is, in html it would have a name but would physically fetch the photo under another name.

Something like:

RewriteRule  ^/imagens/([a-zA-Z0-9_-]+).jpg /imagens/$1.jpg

But it would not be feasible to rename physically in the photos (it has more than 2 million images), it would have to be something like Route or UrlRewrite

    
asked by anonymous 20.08.2015 / 22:18

1 answer

2

Step 1: Configuring the Request Engine

First, you need to tell your application that it will handle file-terminated requests differently. To do this, modify your web.config file by adding the following:

<configuration>
  <system.webServer>
    <handlers>
      ...
      <add name="JpegFileHandler" path="*.jpg" verb="GET" type="System.Web.Handlers.TransferRequestHandler" preCondition="integratedMode,runtimeVersionv4.0" />
    </handlers>
  </system.webServer>
</configuration>

Step 2: Setting the Route

In your App_Start/RouteConfig.cs file, set up a route like this:

routes.MapRoute(
    name: "Fotos",
    url: "{foto}.jpg",
    defaults: new { controller = "Fotos", action = "Obter", foto = UrlParameter.Optional }
);

Step 3: RouteExistingFiles

This prevents files that already exist from being returned with the original name. You can comment on this line later.

routes.RouteExistingFiles = true;

routes.MapRoute(
    name: "Fotos",
    url: "{foto}.jpg",
    defaults: new { controller = "Fotos", action = "Obter", foto = UrlParameter.Optional }
);

Step 4: Controller Method

In FotosController , type the following Obter method:

public ActionResult Obter(string nomedoarquivo)
{
    // Execute aqui sua lógica para recuperar o arquivo.
    return File(variavelComAFotoComoByteArray, "image/jpeg");
}

variavelComAFotoComoByteArray must be byte[] .

    
20.08.2015 / 22:34