In the Index.cshtml file, I'm trying to display the header image in the wwwroot/images folder, so I do
<img src="../../wwwroot/images/header.png" />
but the file won't show when I run the ASP.NET MVC project. I've spent a hours trying to mess around with the file paths and googling but the browser keeps giving me a "failed to load resource" error. What is the filepath?
Screenshot of the project directory
Try this
<img src="~/wwwroot/images/header.png" style="height:200px;width:200px;"/>
Refer these links for more options
How to display an image from a path in asp.net MVC 4 and Razor view?
Image display on MVC view
This will point the path to IIS folder path(C:\Program Files (x86)\IIS Express) if your image is not residing in this path then the image won't get displayed. You might consider using a relative path like below
<img src="#Url.Content("~\\Gallery\\" + item.Source)" />
Related
I have a MVC web application.
The start url is //foo/login/index.
On a view I show a picture images/bar.jpg.
The image is in de directory //foo/images
On my working station this works but when I install it on the test server the site can't find the image because it url to the image is:
//foo/login/images/bar.jpg instead of //foo/images/bar.jpg
So my current directory on my workstation differs from that on the test server.
My question: What determines the current directory in a mvc web application
You probably have something like this in your View:
<img src="images/bar.jpg">
Replace it with:
<img src="~/images/bar.jpg">
The tilde or ~ character is resolved by the rendering engine at runtime to the root (home path) of your website.
I have an IIS web application called "WebApp" created inside the "Default Web Site". The WebApp has a virutal directory with a repository of images called "Repository".
Schema:
-Default Web Site
-WebApp
-Repository
When I execute the web application like this:
http://localhost/WebApp
All my images points to a relative url like "/Repository/ImageDir/image.jpg" which is interpreted like http://localhost/Repository/ImageDir/image.jpg.
That URL is wrong, because since my virtual directory is inside WebApp, the correct URL should be http://localhost**/WebApp**/Repository/ImageDir/image.jpg
How can I make it generate a correct URL.
Just for more info, the web application is in ASP.NET MVC.
UPDATE:
I'm using Razor in my views, so here is the code to generate a img tag:
<img src="#m.ThumbPath" />
Where "m" is an element of the Model (which is a List of elements) and "ThumbPath" contains the relative path that is giving problems.
Since you are using Asp.Net MVC you should be using Url.Content-helper to generate the url to resources inside your app:
<img src="#Url.Content(String.Format("~{0}", m.ThumbPath))">
I have an application that uses ASP.NET MVC. I have it deployed to a Crystal Tech server (hence, no direct access to IIS control panel).
Initialy this was running in the root directory and everything was OK. The client then decided that it needed to run in a subdirectory. I moved the app there and the home index page comes up, but every other page tries to access the the controller/action/page/view in the original root directory. I don't understand this, since the references were all contextual (i. e. using ../controller/action as opposed to mysite.com/controller/action).
Am I doing something wrong here? What are my options?
Thanks,
James
I would use the UrlHelper to generate the links. This would ensure that they are relative to the application path.
Link Text
and
<img src="<%= Url.Content( "~/images/myimg.jpg" ) %>" alt="My Image" />
when specifying the src of an img tag, must i convert the relative path to absolute? please post examples and why not use runat="Server"
Because ASP.NET MVC views are in a separate folder, your best option is to use the
<%= Url.Content("~/ImagesFolder/image.gif") %>
to get the exact path of the image.
All Url.Content does is return the exact path of a specific file using its relative url so it can be used for ANY file in your web application (.js .css .txt etc.).
Alternatively, a better implementation for images is Html.Image() which is available in the MVC Futures (I can't find the .dll link) and I think in the new version of ASP.NET MVC
If you can't find them, check out this post
Where is Html.Image in ASP .NET MVC RC?
<img src="<%=Url.Content("~")%>MyImageFolder/logo.png"/>
runat=server creates a server control in the code behind, it is not really a usefull concept in MVC where you would not want to access properties of the control on the server.
I have dev an MVC app and deployed it to my local IIS as I am using dev server to dev.
This is on Vista Ultimate.
When i browse the site all the images are not showing and also the
login page is displayed.
what would be causing the images not to show and also why
the login page showed when I have not set up security
in web.config?
I tried to see if the ASPNET account had permissions
but there is user of that name and there is no
Add option in properies either.
Malcolm
This could be a deployment issue, rather than a permissions issue. Did you try to browse directly to an image via your browser?
So if you have an image located in your project as
\images\login.png
open in your browser to:
http://hostname/images/login.png
If this works, then you have got a referencing problem in your html. From memory, most images in asp.net mvc are located with:
src="../../images/login.png"
This could break down if your pathing is different to the current location.
I usually prefer this:
src="/images/login.png"
or even better:
src="<%= ResolveUrl("~/images/login.png")%>"
I had a problem with images displaying in an MVC app until I coded the image tags like this:
<img src="<%= Uri.Content("./content/images/image.png") %>" alt="text" />