When you retrieve an image path from Kentico 13 using item.GetProperty(“MyImage”).ToString(), it often returns a path starting with ~ (tilde), which is a virtual path. To use this path in a web context (like an <img> tag src attribute), you need to convert it to an absolute URL.
Example image path: ~/getmedia/8c2d7g7c-9172-5182-6f5f-64d5d30c0e82/MyImage.jpg?width=500&height=250&ext=.jpg
I stumbled over this issue when I was developing an RSS feed which populated the unusable virtual path (missing the domain name).
If you are working within a Kentico 13 MVC application, the most common and recommended way is to use the Url.Content() helper method. This method is designed to convert a virtual path (like one starting with ~) into an application-relative absolute URL.
Here’s how you can get rid of the ~ and obtain a usable URL in Kentico 13 using Url.Content().
string imagePath = item.GetProperty("MyImage").ToString();
string absoluteImageUrl = Url.Content(imagePath);
<img src="@absoluteImageUrl" alt="My Image" />
So. now you get this image path: /getmedia/8c2d7g7c-9172-5182-6f5f-64d5d30c0e82/MyImage.jpg?width=500&height=250&ext=.jpg
All good, but what if I need to get rid of the query string parameters:
?width=500&height=250&ext=.jpg
To get rid of the query string parameters like ?width=500&height=250&ext=.jpg, you can use string manipulation or the System.Uri class in C#. The Uri class is generally more robust for handling URLs, but a simple string method can work too.
Here are a couple of ways to achieve this:
Option 1: Using System.Uri (Recommended for URL manipulation)
This is the most robust way to parse URLs. It correctly handles various URL components.
string imagePathWithTilde = item.GetProperty("MyImage").ToString(); // e.g., "~/getmedia/..."
string absoluteImageUrlWithQuery = Url.Content(imagePathWithTilde); // e.g., "/getmedia/8c2d7g7c-9172-5182-6f5f-64d5d30c0e82/MyImage.jpg?width=500&height=250&ext=.jpg"
// Create a Uri object
Uri uri = new Uri(absoluteImageUrlWithQuery, UriKind.RelativeOrAbsolute);
// Get the path part without the query string
string finalImageUrl = uri.AbsolutePath; // e.g., "/getmedia/8c2d7g7c-9172-5182-6f5f-64d5d30c0e82/MyImage.jpg"
<img src="@finalImageUrl" alt="My Image" />
new Uri(absoluteImageUrlWithQuery, UriKind.RelativeOrAbsolute) – Creates a Uri object from your URL string.
UriKind.RelativeOrAbsolute is important because Url.Content() often returns an application-relative URL (like /getmedia/…), not a full absolute URL (like http://example.com/getmedia/…).
uri.AbsolutePath – This property of the Uri object returns the path component of the URL, excluding the scheme, authority (domain), port, and most importantly for you, the query string and fragment.
Option 2: Using string.IndexOf and string.Substring (Simpler String Manipulation)
If you are confident that the ? will always delineate the start of the query string and you don’t have complex URL scenarios, this is a quick way.
@using Kentico.Web.Mvc
@{
string imagePathWithTilde = item.GetProperty("MyImage").ToString(); // e.g., "~/getmedia/..."
string absoluteImageUrlWithQuery = Url.Content(imagePathWithTilde); // e.g., "/getmedia/8c2d7g7c-9172-5182-6f5f-64d5d30c0e82/MyImage.jpg?width=500&height=250&ext=.jpg"
string finalImageUrl = absoluteImageUrlWithQuery; // Initialize with the full URL
// Find the index of the first '?'
int queryStartIndex = absoluteImageUrlWithQuery.IndexOf('?');
// If a '?' is found, take the substring before it
if (queryStartIndex != -1)
{
finalImageUrl = absoluteImageUrlWithQuery.Substring(0, queryStartIndex); // e.g., "/getmedia/8c2d7g7c-9172-5182-6f5f-64d5d30c0e82/MyImage.jpg"
}
}
<img src="@finalImageUrl" alt="My Image" />
absoluteImageUrlWithQuery.IndexOf(‘?’) – This finds the position of the first ? character in the string.
if (queryStartIndex != -1) – Checks if a ? was actually found. If not, IndexOf returns -1.
absoluteImageUrlWithQuery.Substring(0, queryStartIndex) – If ? is found, this extracts the part of the string starting from the beginning (index 0) up to (but not including) the ? character.
For typical web development, using System.Uri is a good practice as it makes your code more resilient to unexpected URL formats.