I have a class that combines several html files into one document. These files have relative links to images (e.g. <img src="image.jpg">).
I'm converting my combined document to PDF. I need to translate all relative image links to absolute URLs to make images show up in the generated PDF. What I’m trying to do is to get the base URL of my application within class (like http://mydomain.com/app)
and then modify image SRC attribute accordingly. However, I can’t find a way to get the base URL from class.
I can do HttpContext.Current.Server.MapPath("~"), but it gives me back a file path instead of URL: c:\inetpub\wwwroot\app
Could you please tell me if there is a way to do something similar to ResolveUrl("~/")inside class to get the URL, not physical path (ResolveUrl doesn’t work inside class)?
If it not possible, can anyone give me any advise on possible alternative solutions?
You can also set a breakpoint on that line, highlight Request.Url, then right click, then Quickwatch and expand the node to see all the properties in there. One of them should match exactly what you need, or you'll need to concatenate it.
When I try Request.Url.Authority; I get an error message "The name 'Request' dos not exist in a current context". Well, in any way Shane gave me solution for this problem -- thanks.
Ilya22
Member
1 Points
6 Posts
Getting application base URL (similar to ResolveUrl ) from a class
Sep 03, 2009 02:09 PM|LINK
Hello,
I have a class that combines several html files into one document. These files have relative links to images (e.g. <img src="image.jpg">).
I'm converting my combined document to PDF. I need to translate all relative image links to absolute URLs to make images show up in the generated PDF. What I’m trying to do is to get the base URL of my application within class (like http://mydomain.com/app) and then modify image SRC attribute accordingly. However, I can’t find a way to get the base URL from class.
I can do HttpContext.Current.Server.MapPath("~"), but it gives me back a file path instead of URL: c:\inetpub\wwwroot\app
Could you please tell me if there is a way to do something similar to ResolveUrl("~/")inside class to get the URL, not physical path (ResolveUrl doesn’t work inside class)?
If it not possible, can anyone give me any advise on possible alternative solutions?
Thanks!
Shane77E
Member
405 Points
83 Posts
Re: Getting application base URL (similar to ResolveUrl ) from a class
Sep 03, 2009 02:17 PM|LINK
Hi,
Try this:
string.Format("{0}://{1}{2}", HttpContext.Current.Request.Url.Scheme, HttpContext.Current.Request.ServerVariables["HTTP_HOST"], (HttpContext.Current.Request.ApplicationPath.Equals("/")) ? string.Empty : HttpContext.Current.Request.ApplicationPath );Hope this helps,
Shane
<div style="overflow: hidden; position: absolute; left: -10000px; top: 0px; width: 1px; height: 1px;" id="_mcePaste">string.Format("{0}://{1}{2}",
HttpContext.Current.Request.Url.Scheme,
HttpContext.Current.Request.ServerVariables["HTTP_HOST"],
(HttpContext.Current.Request.ApplicationPath.Equals("/")) ? string.Empty : HttpContext.Current.Request.ApplicationPath
);</div>
mendhak
All-Star
17868 Points
2787 Posts
Re: Getting application base URL (similar to ResolveUrl ) from a class
Sep 03, 2009 02:19 PM|LINK
Try
Request.Url.Authority
You can also set a breakpoint on that line, highlight Request.Url, then right click, then Quickwatch and expand the node to see all the properties in there. One of them should match exactly what you need, or you'll need to concatenate it.
Ilya22
Member
1 Points
6 Posts
Re: Getting application base URL (similar to ResolveUrl ) from a class
Sep 03, 2009 02:39 PM|LINK
Ilya22
Member
1 Points
6 Posts
Re: Getting application base URL (similar to ResolveUrl ) from a class
Sep 03, 2009 02:43 PM|LINK
When I try Request.Url.Authority; I get an error message "The name 'Request' dos not exist in a current context". Well, in any way Shane gave me solution for this problem -- thanks.