The right approach would be to create an appropriate interface to serve the images on the administrative site.
To do this, create a Controller named ImagensController
. Within it implement the following Action :
public FileResult Imagem(int id)
{
if (id != null)
{
var imagem = db.Imagens.FirstOrDefault(x => x.ImagemId == id);
if (imagem != null)
{
var arquivoDeImagem = Server.MapPath("~/Content/images/galeria/" + imagem.imagem);
return File(arquivoDeImagem, "image/jpeg");
}
}
return null;
}
Usage:
<img src="http://siteadministrativo/Imagens/Imagem/5"alt="@item.nome" />
Note that this method uses an image register within your administrative module, since we select the register of an image in a data context ( db
).
Also, if you want to leave the configuration very dynamic, you can use a configurable URL in your Web.config
:
<configuration>
...
<appSettings>
...
<add key="BaseURL" value="http://localhost:15829/" />
...
</appSettings>
...
</configuration>
To access the value in View , you can implement a Helper :
public static class ConfigurationHelper
{
public static String BaseUrl()
{
var baseUrl = ConfigurationManager.AppSettings["BaseURL"];
return baseUrl;
}
}
There you will find:
<img src="@(ConfigurationHelper.BaseUrl() + "/Imagens/Imagem/5")" alt="@item.nome" />
Finally, you can use transformation files and set up URL swapping at the time of publication:
<configuration xmlns:xdt="http://schemas.microsoft.com/XML-Document-Transform">
<appSettings>
<add key="BaseURL" value="http://meusite.com.br" xdt:Transform="SetAttributes" xdt:Locator="Match(key)" />
</appSettings>
<system.web>
<compilation xdt:Transform="RemoveAttributes(debug)" />
</system.web>
</configuration>