Using Url.Content with semi-relative URL

后端 未结 2 1225
情深已故
情深已故 2020-12-31 02:54

I am storing the location of images in my database in an MVC application...but i am only storing part of the location. For example:

/headers/image1.jpg
/head         


        
相关标签:
2条回答
  • 2020-12-31 03:09

    You can write extension method which combine your ImageUrl with configured path to content directory.

    <img src="@Model.ImageUrl.ToRelative()" alt="@Model.ImageAlt" />
    

    PS
    Remember about alt tag. ;)

    0 讨论(0)
  • 2020-12-31 03:16

    Just do it!

    <img src="@Url.Content("~/content/images" + Model.ImageUrl)" />
    

    UPDATE:

    As of ASP.NET MVC 4 it is valid to use the tilde URLs directly in HTML as the Razor View Engine will parse the URLs. Like this:

    <img src="~/content/images/@Model.ImageUrl" />
    
    0 讨论(0)
提交回复
热议问题