<?xml version="1.0" encoding="utf-8"?><rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
		>
<channel>
	<title>Comments on: Get Application Path [C# / ASP.NET]</title>
	<atom:link href="http://www.thejackol.com/2004/09/22/get-application-path-c-aspnet/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.thejackol.com/2004/09/22/get-application-path-c-aspnet/</link>
	<description>01100010 01101100 01100101 01101000</description>
	<lastBuildDate>Tue, 07 Feb 2012 21:10:47 +0000</lastBuildDate>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.3.1</generator>
	<item>
		<title>By: arash</title>
		<link>http://www.thejackol.com/2004/09/22/get-application-path-c-aspnet/comment-page-1/#comment-217541</link>
		<dc:creator>arash</dc:creator>
		<pubDate>Sat, 10 Oct 2009 12:26:38 +0000</pubDate>
		<guid isPermaLink="false">http://www.thejackol.com/?p=42#comment-217541</guid>
		<description>string relative = System.Web.HttpContext.Current.Request.Url.AbsoluteUri;
        relative = relative.Substring(0, relative.LastIndexOf(&quot;/&quot;));</description>
		<content:encoded><![CDATA[<p>string relative = System.Web.HttpContext.Current.Request.Url.AbsoluteUri;<br />
        relative = relative.Substring(0, relative.LastIndexOf(&#8221;/&#8221;));</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: NAME</title>
		<link>http://www.thejackol.com/2004/09/22/get-application-path-c-aspnet/comment-page-1/#comment-203547</link>
		<dc:creator>NAME</dc:creator>
		<pubDate>Wed, 10 Jun 2009 03:10:30 +0000</pubDate>
		<guid isPermaLink="false">http://www.thejackol.com/?p=42#comment-203547</guid>
		<description>Save yourselves some time:
HttpContext.Current.Request.PhysicalApplicationPath</description>
		<content:encoded><![CDATA[<p>Save yourselves some time:<br />
HttpContext.Current.Request.PhysicalApplicationPath</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: eok</title>
		<link>http://www.thejackol.com/2004/09/22/get-application-path-c-aspnet/comment-page-1/#comment-181735</link>
		<dc:creator>eok</dc:creator>
		<pubDate>Mon, 24 Nov 2008 22:36:50 +0000</pubDate>
		<guid isPermaLink="false">http://www.thejackol.com/?p=42#comment-181735</guid>
		<description>Request.Url.GetLeftPart(UriPartial.Authority) + Page.ResolveUrl(&quot;~/Page.aspx&quot;)</description>
		<content:encoded><![CDATA[<p>Request.Url.GetLeftPart(UriPartial.Authority) + Page.ResolveUrl(&#8221;~/Page.aspx&#8221;)</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Steve71</title>
		<link>http://www.thejackol.com/2004/09/22/get-application-path-c-aspnet/comment-page-1/#comment-171393</link>
		<dc:creator>Steve71</dc:creator>
		<pubDate>Fri, 17 Oct 2008 14:31:06 +0000</pubDate>
		<guid isPermaLink="false">http://www.thejackol.com/?p=42#comment-171393</guid>
		<description>My personal method, not as short as others, but fail-proof and ultra-tested. Bye!

public static class clsHTTPManager
{
    public static string GetApplicationUrl()
    {
        HttpContext ctx = HttpContext.Current;

        string toReturn = (string)ctx.Cache.Get(&quot;ApplicationUrl&quot;);

        if (String.IsNullOrEmpty(toReturn))
        {
            string protocol = ctx.Request.ServerVariables.Get(&quot;SERVER_PORT_SECURE&quot;);
            string port = ctx.Request.ServerVariables.Get(&quot;SERVER_PORT&quot;);
            string host = ctx.Request.ServerVariables.Get(&quot;SERVER_NAME&quot;);

            if (protocol.Equals(&quot;off&quot;, StringComparison.OrdinalIgnoreCase) &#124;&#124; protocol.Equals(&quot;0&quot;))
                protocol = &quot;http://&quot;;
            else
                protocol = &quot;https://&quot;;

            if (port.Equals(&quot;80&quot;))
                port = String.Empty;
            else
                port = &quot;:&quot; + port;

            toReturn = String.Format(&quot;{0}{1}{2}{3}&quot;, protocol, host, port, ctx.Request.ApplicationPath);

            ctx.Cache.Insert(&quot;ApplicationUrl&quot;, toReturn);
        }

        return toReturn;
    }
}</description>
		<content:encoded><![CDATA[<p>My personal method, not as short as others, but fail-proof and ultra-tested. Bye!</p>
<p>public static class clsHTTPManager<br />
{<br />
    public static string GetApplicationUrl()<br />
    {<br />
        HttpContext ctx = HttpContext.Current;</p>
<p>        string toReturn = (string)ctx.Cache.Get(&#8220;ApplicationUrl&#8221;);</p>
<p>        if (String.IsNullOrEmpty(toReturn))<br />
        {<br />
            string protocol = ctx.Request.ServerVariables.Get(&#8220;SERVER_PORT_SECURE&#8221;);<br />
            string port = ctx.Request.ServerVariables.Get(&#8220;SERVER_PORT&#8221;);<br />
            string host = ctx.Request.ServerVariables.Get(&#8220;SERVER_NAME&#8221;);</p>
<p>            if (protocol.Equals(&#8220;off&#8221;, StringComparison.OrdinalIgnoreCase) || protocol.Equals(&#8220;0&#8221;))<br />
                protocol = &#8220;<a href="http://&#8221;" rel="nofollow">http://&#8221;</a>;<br />
            else<br />
                protocol = &#8220;<a href="https://&#8221;" rel="nofollow">https://&#8221;</a>;</p>
<p>            if (port.Equals(&#8220;80&#8221;))<br />
                port = String.Empty;<br />
            else<br />
                port = &#8220;:&#8221; + port;</p>
<p>            toReturn = String.Format(&#8221;{0}{1}{2}{3}&#8221;, protocol, host, port, ctx.Request.ApplicationPath);</p>
<p>            ctx.Cache.Insert(&#8220;ApplicationUrl&#8221;, toReturn);<br />
        }</p>
<p>        return toReturn;<br />
    }<br />
}</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Quang Vo</title>
		<link>http://www.thejackol.com/2004/09/22/get-application-path-c-aspnet/comment-page-1/#comment-164904</link>
		<dc:creator>Quang Vo</dc:creator>
		<pubDate>Wed, 24 Sep 2008 09:32:17 +0000</pubDate>
		<guid isPermaLink="false">http://www.thejackol.com/?p=42#comment-164904</guid>
		<description>You can try it this way and it works great. http://connectionstringexamples.com/article.php?story=20080924032917497</description>
		<content:encoded><![CDATA[<p>You can try it this way and it works great. <a href="http://connectionstringexamples.com/article.php?story=20080924032917497" rel="nofollow">http://connectionstringexamples.com/article.php?story=20080924032917497</a></p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Gauthier Segay</title>
		<link>http://www.thejackol.com/2004/09/22/get-application-path-c-aspnet/comment-page-1/#comment-141770</link>
		<dc:creator>Gauthier Segay</dc:creator>
		<pubDate>Sun, 13 Jul 2008 20:11:54 +0000</pubDate>
		<guid isPermaLink="false">http://www.thejackol.com/?p=42#comment-141770</guid>
		<description>This is so scary how unfriendly ASP.NET can be...

I&#039;ve refactored the function to something that is Page agnostic and with less duplication of unecessary property access:

public string GetApplicationAbsolutePath(Uri requestUri, string siteRootRelativeApplicationPath) {
	string 
		requestAbsoluteUri = requestUri.AbsoluteUri,
		requestAuthority = requestUri.Authority;
	return
		requestAbsoluteUri.Substring(0,
			requestAbsoluteUri.IndexOf(
				siteRootRelativeApplicationPath.ToLower(),
				requestAbsoluteUri.IndexOf(
					requestAuthority, 
					StringComparison.InvariantCultureIgnoreCase
				) 
				+ requestAuthority.Length,
				StringComparison.InvariantCultureIgnoreCase
			) 
			+ siteRootRelativeApplicationPath.Length
			);
}</description>
		<content:encoded><![CDATA[<p>This is so scary how unfriendly <span class="caps">ASP</span>.<span class="caps">NET</span> can be&#8230;</p>
<p>I&#8217;ve refactored the function to something that is Page agnostic and with less duplication of unecessary property access:</p>
<p>public string GetApplicationAbsolutePath(Uri requestUri, string siteRootRelativeApplicationPath) {<br />
	string<br />
		requestAbsoluteUri = requestUri.AbsoluteUri,<br />
		requestAuthority = requestUri.Authority;<br />
	return<br />
		requestAbsoluteUri.Substring(0,<br />
			requestAbsoluteUri.IndexOf(<br />
				siteRootRelativeApplicationPath.ToLower(),<br />
				requestAbsoluteUri.IndexOf(<br />
					requestAuthority,<br />
					StringComparison.InvariantCultureIgnoreCase<br />
				)<br />
				+ requestAuthority.Length,<br />
				StringComparison.InvariantCultureIgnoreCase<br />
			)<br />
			+ siteRootRelativeApplicationPath.Length<br />
			);<br />
}</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Allan McLemore</title>
		<link>http://www.thejackol.com/2004/09/22/get-application-path-c-aspnet/comment-page-1/#comment-116995</link>
		<dc:creator>Allan McLemore</dc:creator>
		<pubDate>Sat, 05 Apr 2008 17:23:20 +0000</pubDate>
		<guid isPermaLink="false">http://www.thejackol.com/?p=42#comment-116995</guid>
		<description>I&#039;ll have to try this methodology.  You can use Singleton pattern on this and persist the Application path in an object property. That way the evaluation only happens once in each application instance.

I took a slightly different approach to solving this issue.  When you try to generate Urls using the various values built into ASP.NET (ApplicationPath, &quot;~&quot;, etc), you will encounter problems in some scenarios.

Check out my post on this issue:  http://www.zenternal.com/weblog/?p=6

The code sample provided in my article does not address the issue with ASP.NET returning localhost when running on XP Workstation.  However, the URL methodology in the code can be improved to use System.Environment.MachineName whenever Request.ServerVariables[&quot;SERVER_NAME&quot;] returns localhost.  I&#039;ll try to post an update to the article soon.</description>
		<content:encoded><![CDATA[<p>I&#8217;ll have to try this methodology.  You can use Singleton pattern on this and persist the Application path in an object property. That way the evaluation only happens once in each application instance.</p>
<p>I took a slightly different approach to solving this issue.  When you try to generate Urls using the various values built into <span class="caps">ASP</span>.<span class="caps">NET</span> (ApplicationPath, &#8220;~&#8221;, etc), you will encounter problems in some scenarios.</p>
<p>Check out my post on this issue:  <a href="http://www.zenternal.com/weblog/?p=6" rel="nofollow">http://www.zenternal.com/weblog/?p=6</a></p>
<p>The code sample provided in my article does not address the issue with <span class="caps">ASP</span>.<span class="caps">NET</span> returning localhost when running on XP Workstation.  However, the <span class="caps">URL</span> methodology in the code can be improved to use System.Environment.MachineName whenever Request.ServerVariables[&#8220;SERVER_NAME&#8221;] returns localhost.  I&#8217;ll try to post an update to the article soon.</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Ken</title>
		<link>http://www.thejackol.com/2004/09/22/get-application-path-c-aspnet/comment-page-1/#comment-116392</link>
		<dc:creator>Ken</dc:creator>
		<pubDate>Tue, 01 Apr 2008 19:54:46 +0000</pubDate>
		<guid isPermaLink="false">http://www.thejackol.com/?p=42#comment-116392</guid>
		<description>This requires less code:

Request.Url.AbsoluteUri.Substring(0, Request.Url.AbsoluteUri.LastIndexOf(&quot;/&quot;))</description>
		<content:encoded><![CDATA[<p>This requires less code:</p>
<p>Request.Url.AbsoluteUri.Substring(0, Request.Url.AbsoluteUri.LastIndexOf(&#8221;/&#8221;))</p>
]]></content:encoded>
	</item>
</channel>
</rss>

