How do I get the absolute media file path in umbraco using razor?
I'm not sure whether there is a solution within Umbraco, I'd just use the .NET framework. With ASP.NET, you can use MapPath to resolve virtual paths to physical file paths:
@HttpContext.Current.Server.MapPath(room.Media("summaryImage","umbracoFile"))
EDIT:
If you are looking for the absolute URL, you may use one of the following variants:
@Page.ResolveUrl(room.Media("summaryImage","umbracoFile"))
or
@VirtualPathUtility.ToAbsolute(room.Media("summaryImage","umbracoFile"))
You may would like to read this article about different approaches for resolving URLs.
zlog
I’m a London based web developer, currently working at pebble {code}.
Updated on June 05, 2022Comments
-
zlog almost 2 years
I've tried the following bit of razor code:
@room.Media("summaryImage","umbracoFile")
but I get something like
~/media/155/lux.jpg
, how do I remove the initial~
to get a server path ie,/media/155/lux.jpg
orhttp://some_url/media/155/lux.jpg
?EDIT:
I've tried
@{ dynamic summaryImagePath = room.Media("summaryImage","umbracoFile"); } @Page.ResolveUrl(@summaryImagePath)
and
@Page.ResolveUrl(@room.Media("summaryImage","umbracoFile"))
but I keep getting the error:
Error loading Razor Script Cannot perform runtime binding on a null reference
even though
@room.Media("summaryImage","umbracoFile")
gives~/media/155/lux.jpg
.Any ideas?
-
zlog over 12 yearsUsing this I get something like C:\inetpub\wwwroot[path to media]\media\155\lux.jpg. I just want the /media/155/lux.jpg bit. I've updated the question to reflect this.
-
zlog over 12 yearsPage.ResolveUrl gave me a null reference error, but VirtualPathUtility.ToAbsolute worked like a charm!
-
zlog over 12 yearsMaybe it's because razor scripts don't have a Page context as per: refactoringaspnet.blogspot.com/2009/09/…