A
A
akass2016-02-26 01:48:10
ASP.NET
akass, 2016-02-26 01:48:10

What is the correct path to images in ASP.NET MVC 5?

I store the paths to the images in the database, they themselves lie separately.
How to correctly write the path to the image? No matter how he wrote, he did not see them.
It is desirable that when publishing everything normally flew away and also worked.
That is, as I understand it, a relative path is needed, but "~/" does not work.

Answer the question

In order to leave comments, you need to log in

3 answer(s)
D
Dmitry Kovalsky, 2016-02-26
@akass

It depends on what you consider the norm and "everything has flown normally."
1) you can store in the database (and not only you can but also store) the identifiers of the image data. Accordingly, you can store them in a shared folder and sign only the image ID in the path to the image.
2) Another way is to write a small controller that renders images according to your logic. The path to the image will be approximately the same for everyone - controllerName/GetImage/ImageId , and where you get this image on the server is your own business (this method is heavy for a large number of images.
3) Suppose that all your images in a certain structure lie along the Content path /SomeFolder/SomeImage
you can write a tag like this - <img src = "Content/@ImagePath", and store the ImagePath in the database with the line SomeFolder/SomeImage.
In my opinion, all these ways have their drawbacks.

P
Peter, 2016-02-26
@petermzg

1. Store images in directories that are accessed via IIS (or other web server), example: "~/Images" of the project.
2. Give the contents of the file through the created controller, then you specify the path to the controller on the page, and it already gives the content of the file, along the path specified from the entry in the database.

S
Sergey, 2016-04-01
Gorbulev @gorbulevsv

Hi guys! I'm new to MVC (I wanted to thank Dimitri Kowalski for his answers to questions in my profile, and for encouraging me to try switching to MVC).
I also suffered with inserting a picture, and dug out such a solution, it seems to work:
In the controller, I form the address to the picture in this way:
And another option:

string src=Url.Content("~/Content/фотографии/фотография.jpg")

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question