locked
beginner question RRS feed

  • Question

  • User1457412228 posted

    I am following this page https://docs.microsoft.com/en-us/aspnet/core/fundamentals/static-files?view=aspnetcore-2.2

    and I have added the line  app.UseStaticFiles(); to my configuration, but the address "~/images/myfile.jpeg does not find the images folder.  However, the address "~/../../images/myfile.jpeg" does find it on my local server, but not on my website.

    It say in the link that "~/" is supposed to represent www.root but it appears to behave like regular file system addresses. 

    How can I relatively address my path?

    src=@Model.Styles[j].ImageFileName />
    private List<ResumeStyle> UpdateImagePathForStyles(List<ResumeStyle> styles, string folder)
            { 
                for (int i = 0; i < styles.Count; i++)
                {
                    styles[i].ImageFileName = $"~/images/{styles[i].ImageFileName}";
                }
                return styles;
            }

    thanks

    Tuesday, August 20, 2019 2:56 PM

Answers

  • User-474980206 posted

    support of "~/" is provided by server code, via tag helpers (don't know the tag you are using). try:

    src=@Url.Content(Model.Styles[j].ImageFileName) />

    a view source should show you if the correct url is produced.

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Tuesday, August 20, 2019 4:03 PM
  • User-2054057000 posted

    You can also go to the root by just using "/":

    styles[i].ImageFileName = $"/images/{styles[i].ImageFileName}";


     

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Wednesday, August 21, 2019 5:58 AM
  • User1289604957 posted

    Hi,

    There is a similar thread <g class="gr_ gr_59 gr-alert gr_gramm gr_inline_cards gr_run_anim Style multiReplace" id="59" data-gr-id="59">here :</g> https://forums.asp.net/t/2158699.aspx?API+Method+to+Return+images+xyz+png+is+returning+api+images+xyz+png+

    Best regards,

    Maher

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Wednesday, August 21, 2019 10:50 AM

All replies

  • User-474980206 posted

    support of "~/" is provided by server code, via tag helpers (don't know the tag you are using). try:

    src=@Url.Content(Model.Styles[j].ImageFileName) />

    a view source should show you if the correct url is produced.

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Tuesday, August 20, 2019 4:03 PM
  • User1457412228 posted

    Thank you.  The line produces this src=/images/myimage.jpeg 

    It is resolved on localhost but does not resolve on the hosted website

    Most things I try work to display the image for localhost and NOTHING works for hosted website.  It is odd.

    Wednesday, August 21, 2019 3:03 AM
  • User-2054057000 posted

    You can also go to the root by just using "/":

    styles[i].ImageFileName = $"/images/{styles[i].ImageFileName}";


     

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Wednesday, August 21, 2019 5:58 AM
  • User1289604957 posted

    Hi,

    There is a similar thread <g class="gr_ gr_59 gr-alert gr_gramm gr_inline_cards gr_run_anim Style multiReplace" id="59" data-gr-id="59">here :</g> https://forums.asp.net/t/2158699.aspx?API+Method+to+Return+images+xyz+png+is+returning+api+images+xyz+png+

    Best regards,

    Maher

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Wednesday, August 21, 2019 10:50 AM
  • User1457412228 posted

    Thank you for your answers

    I am assuming now the image I was testing with was not being deployed to the server.  It works with a different image.  However, for the few files I have tested, it will not work on the web server with jpg images I tested but will work with png. 

    I also converted the image to a png and then it works (and resized to 25%) on the server.  I havent tested it enough to know but it is as if it cannot serve either jpg or jpeg on the server, but can locally.

    Is there a way to know for sure what is in the images folder on the server, so this doesn't happen again?  I have the image that cannot be found in both the wwwroot/images folder and the project images folder.

    thanks

    Wednesday, August 21, 2019 1:16 PM