Using Url.Content with semi-relative URL - asp.net-mvc

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
/headers/image2.jpg
The images are actually stored in the following folder:
~/content/images/headers/image1.jpg
~/content/images/headers/image1.jpg
In my view, I want to do something like this:
<img src="#Url.Content("~/content/images") + Model.ImageUrl" />
How can I do this?

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" />

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. ;)

Related

Url.Content is not showing image in razor view

I have a model class Registration in which I have a photo property which is saving the URL of the photo, my photos are saved in the images folder onto the root.
Now when I am trying to show the images it is not showing the image I have cross verified that the image is present there.
I have tried <img src="#item.Photo" width="100" height="100" />
#*<img src="#item.Photo" width="100" height="100" />*#
also I have tried
<img src="#Url.Content(#item.Photo)" height="25px" width="50px" />
my path becomes /imges/previous theme.jpg even adding ~ in start it is still not showing the result, I have images folder onto the root and inside that I have previous theme.jpg file
It should show the result as image, even after removing the url.Content
<img src="#item.Photo" />
is also not working.
#Url.Content(#item.Photo) is likely the binary representation of your image. An img src= tag must contain a url to the image, not the binary representation of the image itself
i think you should press F12, in the console tab you can see your wrong path image, and you can fix the path base on it.
Razor has been changed a little with ASP.NET MVC 4 beta.No more #Url.Content.
Now, instead of
<img src="#Url.Content("~/path")">
you can use
<img src="~/path">
Kindly check what you are getting in the console. check the path is correct.

Why I get error when I try to display image?

I work use asp.net mvc5 in my project.
I try to display Image stored in my Shared folder in the project:
<img src="#Url.Content(~/Views/Shared/logo.png)" alt="" />
But I get this error on the fly:
Compiler Error Message: CS1525: Invalid expression term '/'
Any idea why I get the error above?
#Url.Content helper needs string as a parameter so you should write like this:
<img src="#Url.Content("~/Views/Shared/logo.png")" alt="" />
or you can use single brackets
<img src='#Url.Content("~/Views/Shared/logo.png")' alt='' />

what is the difference between grails link ,createlink and resource tags

Is there any difference between grails link ,create link and resource tags and I want to know when to use each tag as per my knowledge the createlink tag has depricated
it's simple:
g.createLink generates only the url, like /aaa/bbb/222, based on controller/action/params for example
g.link generated the <a>-HTML-tag, using the g.createLink to generate the url to be put into the #href attribute
g.resource outputs a path to a resource file, available under web-app folder
<link rel="stylesheet" href="${resource(dir:'css',file:'style.css')}" />
produces
<link rel="stylesheet" href="/css/style.css" />
CREATE LINK is soemthing power full when u come to knowing the absolute or relative path in using likes inside a gsp page.
Let assume i have the following path /yourapp/controller1/view1
Instead of using
<a href="{grailscontext.thensomebaseurlstuff}/"+controller/view /> ,
which fails according to some context using the below will make it easy.
my link
<g:createLink url="[action:'list',controller:'book']" />
And ,glink is the above implementation using taglib form.. does the same effect as above but being in taglib makes reduce some computation or create some am not sure.
g.link("text of the link here", action:"foo", controller:"bar")
ResourceTags is no wonder its something help full and important in attaching either an image,css and js folder/file resource into a grails application.
// generates "/shop/css/main.css"
<g:resource dir="css" file="main.css" />
// generates "http://portal.mygreatsite.com/css/main.css"
<g:resource dir="css" file="main.css" absolute="true" />
// generates "http://admin.mygreatsite.com/css/main.css"
<g:resource dir="css" file="main.css" base="http://admin.mygreatsite.com"/>
Alloha!

How to ecape the "." character in MVC Razor

I have a MVC view where I need to render something like this:
<img src="foo/#(Model.Id).jpg" />
The problem is that Razor thinks the dot in ".jpg" belongs to the previous statement and thus tells me that there are no such field or property called "jpg". How can I overcome this?
Try this version, this would probably work
Edited:
<img src=#("foo/" + Model.Id + ".jpg") /> <!-- Try this simplified version -->
Your example works fine for me and display correct html with ID in it foo/51.jpg. 51 is number from my model.
But just as another idea you can save it as local variable like this:
#{
string path = "foo/" + Model.Id + ".jpg";
}
<img src="#path" />

MVC 3 - _Layout.cshtml. Add a logo to the site

I'm new to MVC 3, I'm trying to add an Image to the _Layout.cshtml file.
I tried
<img runat="server" id="img_logo" alt="Logo" src="Content/Images/mailworks.png" />
no success. The logo only appear on some views. on others views for some reason the image is supposed to be in some other location - found it using firebug.
Try this:
<img id="img_logo" alt="Logo" src="#Url.Content("~/Content/Images/mailworks.png")" />
Use this:
<img src="#Url.Content("~/Content/Images/mailworks.png")"...
I found another quick solution : just append '/' at the beginning of the src's path.

Resources