Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ASP.NET MVC images and other static content url

I just edited my route for a user details page to look like this:

        routes.MapRoute(
            "UserDetails", // Route name
            "{controller}/{action}/{id}/{title}", // URL with parameters
            new { controller = "Users", action = "Details", id = UrlParameter.Optional, title = UrlParameter.Optional } // Parameter defaults
            );

Now when my url looks like this: localhost/Users/Details/1/ShawnMclean Images do not load both from the controller and the site.master. (no idea why the css and javascript had correct urls though). If the url is localhost/Users/Details/1 then everything loads fine.

My img in site.master and Details.aspx looks like this in the old url:

<img src="../../Content/Images/logo3.png" />

but when the url gets an additional parameter, the image is actually located at ../../../Content/Images/logo3.png

Is there a way to make images and other static content's url change?

like image 948
Shawn Mclean Avatar asked Oct 22 '10 21:10

Shawn Mclean


People also ask

How to display image in ASP NET MVC?

Here we will learn that to display image in ASP.NET MVC. Step 1 - Go to SQL Server Management System and execute the following script. Step 2 - Go to Visual studio and add a new project.

How to upload and retrieve images in ASP NET Core MVC?

So let’s look at how to upload and retrieve images in ASP.NET Core MVC. Save the image to the server file. Retrieve uploaded back to the view. Delete image record and file. In Visual Studio 2019, Go to File > New > Project (Ctrl + Shift + N). Select Asp.Net Core Web Application template. Once you provide the project name and location.

Where do I put static files in ASP NET Core?

Folder Location to keep Static Files (Images, CSS and JS files) in ASP.Net Core In the below screenshot, three folders CSS, Images and Scripts have been created inside the wwwroot folder. The wwwroot folder is the default folder provided by ASP.Net Core for storing all the Static files such as Image files, CSS files and JavaScript JS files.

What is image server control in ASP NET?

An ASP.NET Image server control is a control that displays an image on the web page. The image is sourced from either a location on the server or from a URL on the internet. Why did I use the term server control?


3 Answers

Try linking your images like this:

<img src="/Content/Images/logo3.png" /> 

or if that doesn't work you could always use a helper for your links

<img src="<%= Url.Content("~/Content/Images/logo3.png") %>" />

one other way could be

<img src="@Url.Content("~/Content/Images/logo3.png")" />
like image 102
quakkels Avatar answered Oct 18 '22 22:10

quakkels


You can try using a helper:

<img src='<%= Url.Content( "~/Content/Images/pic.jpg" ) %>' alt="My Image" />
like image 24
Justin Soliz Avatar answered Oct 18 '22 22:10

Justin Soliz


You can try this,

<a href="/"><img src="<%=Url.Content("~/Content/Images/logo.png")%>" alt="logo" title="Logo" /></a> 
like image 1
Duk Avatar answered Oct 18 '22 22:10

Duk