Access content from another project in the same Solution

5

The case is as follows: I have a Solution with two Projects, one Administrative and the other a WebSite, both using C # and MVC4.

In the first project I save images and files in a Documents folder, and when accessing the WebSite I would like to access the contents of this folder. I understand that Solution should have a virtual directory and that I should configure it when hosting my application and website, but I do not know how I can simulate this while they are not yet hosted.

I've even used @Url.Content("~/Documents/"+cliente.Imagem) but with no success. I believe I am not fully understanding the scenario and / or how I should prepare it.

Thank you in advance.

    
asked by anonymous 24.08.2015 / 20:04

3 answers

2

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>
    
17.09.2015 / 17:01
1

You will have to upload the two projects at the same time, that is, the two have to be "Online". Your reference to imagen should be the complete url example: h t t p: //localhost:8455/Content/imagen01.jpg

to access:

    
03.09.2015 / 23:30
0

Gentlemen, thank you very much for your help, I have discovered the solution and I believe that it can help many. What I did was run the administrative first, so it "simulated a server like link ..... Then I inserted the full path of the image, including the localhost + port and it worked, look in the example example: @ Url.Content (" link " + item.Image) I accessed the above content through the following URL: link Thanks again for the force !!!

    
04.09.2015 / 23:57