Access content from another project in the same Solution

Asked

Viewed 2,406 times

5

Here’s the thing: I have a Solution with two Projects, one Administrative and one Website, both using C# and MVC4.

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

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

Thanks in advance.

  • 1

    You need to access the physical address of the folder where you are saving the documents... If your application (website) has permission to access the physical folder that you store the documents just go on with life...

  • In case Full physical address as below? C: Users VICTOR Solution Admin Documents[document name]?

  • 1

    If this is the folder where you keep the documents, yes.

  • Hello, I tried the physical path as suggested, the folder is allowed. However the image cannot be found yet. I’ve tried with Bars and Counter-Bars tbm, but none of them successfully. Have an example LINK to make sure I’m not missing? Thanks

  • Look, there’s no magic: they’re files in a folder, that’s all. For example, when you add a file in the c: images folder, there will be the images and you can access them from anywhere as long as you have access/permission. First of all, can you go to the folder and make sure the images are really there? The second step, confirmed the first, would be to access all the files from the folder via code, to see if the user who is running the application actually has permission to view the files. If yes, be happy, otherwise I believe it’s permission or the code...

  • that cliente.imagem comes with the file extension?

Show 1 more comment

3 answers

2


The correct approach would be to create, on the administrative site, a suitable interface to serve the images.

To do this, create a Controller called ImagensController. Inside 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;
}

Use:

<img src="http://siteadministrativo/Imagens/Imagem/5" alt="@item.nome" />

Note that this method uses an image registration inside its administrative module, because we selected the image registration in a data context (db).

Still, if you want to make 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 on View, you can implement a Helper:

public static class ConfigurationHelper
{
    public static String BaseUrl()
    {
        var baseUrl = ConfigurationManager.AppSettings["BaseURL"];
        return baseUrl;
    }
}

There it stands:

<img src="@(ConfigurationHelper.BaseUrl() + "/Imagens/Imagem/5")" alt="@item.nome" />

Finally, you can use transformation files and configure the URL exchange 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>
  • 1

    Thank you very much, your reply has made my code much more streamlined, as well as learning in a better way.

1

You will have to climb both projects at the same time, IE, both have to be "Online". Your image reference should be the full url example: h t t p: //localhost:8455/Content/imagen01.jpg

to access:

inserir a descrição da imagem aqui inserir a descrição da imagem aqui

  • This doesn’t exactly solve the problem that the questioner has, but it’s a great tip. + 1.

0

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

  • This solution is not good, because if you publish both sites elsewhere, it will not work.

  • My friend, it was for testing purposes, because when I create a virtual directory in my hosting I will not have but this problem. But if you have a better suggestion you could let me know?

  • I do. I can put as an answer if you like. It takes a little more work, but it is more correct and more robust.

  • Sure, feel free. Anything I change Check to your :D

Browser other questions tagged

You are not signed in. Login or sign up in order to post.