0

I am creating a web site with ASP.NET Web pages. I need to set path for html img tag dynamically. I searched everywhere they have given reasonable answeres but still they didnt work for me.

Image file path - root/Images/11.jpg

My code in Upload.cshtml

@{
    var fileName = "11.jpg";
    var path = "Images/" + fileName;

    <img src="@path" alt="image" />
}

I even tried

<img src=@path alt="image" />

Still it doesn't take the value from variable.

3 Answers 3

1

Try this

@{  
  string imagePath = Url.Content("~/Images/11.jpg");
}

Then your img tag can look like

<img src="@imagePath" alt="image" />
Sign up to request clarification or add additional context in comments.

1 Comment

I get "Url doesnt exist in current context error" . I serche for that error too but didnt get satisfying answer .
0

I use it like this:

@{
  string iSrc = "~/imagePath/11.jpg"; //The src path.
}

<img src='@Url.Content(iSrc)' alt="img" />

The above answer from @izzy should work as well.

2 Comments

I get "Url doesnt exist in current context error" . I serche for that error too but didnt get satisfying answer .
hmm.. I might be wrong but I think you might have an issue with writing razor syntax in your views. Are you able to write in your view @Url. and get a pre-populated list of methods i.e. Action, Content, Encode, eqauls etc? if that is the case, check this link out: stackoverflow.com/questions/27229492/…
0

correct code var path = "/Images/" + fileName;

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.