{"id":982,"date":"2010-09-07T14:22:34","date_gmt":"2010-09-07T08:52:34","guid":{"rendered":"http:\/\/JitendraZaa.com\/blog\/?p=982"},"modified":"2010-09-07T14:22:34","modified_gmt":"2010-09-07T08:52:34","slug":"create-url-dynamically-in-asp-net","status":"publish","type":"post","link":"https:\/\/www.jitendrazaa.com\/blog\/microsoft\/net\/create-url-dynamically-in-asp-net\/","title":{"rendered":"Create URL dynamically in ASP.NET"},"content":{"rendered":"<p>Hi friend,<\/p>\n<p>I am\u00a0writing\u00a0this article after very long duration. Have lots of tips to share from my toolbox, so here few about the dynamic creation of URL in ASP.NET:<\/p>\n<p>lets say, you have to register the javascript file from code behind, then you may need a full path for that js file in format &#8220;http:\/\/yourdomain\/jsfolder\/jsfilename&#8221;<\/p>\n<p>one way to achieve this is like :<\/p>\n<pre class=\"brush: csharp; title: ; notranslate\" title=\"\">\n&quot;http:\/\/&quot; Request.ServerVariables&#x5B;&quot;HTTP_HOST&quot;] + &quot;\/jsfolder\/jsfilename.js&quot;;\n<\/pre>\n<p>In above snap you can see that HTTP_HOST is used. This server variable returns the address of your domain. Still, above code is not 100% functional.protocol may be <strong>http <\/strong>or <strong>https<\/strong>.<\/p>\n<p><!--more--><\/p>\n<p>To get the exact protocol, we need to check that connection is secured or not.<br \/>\nbelow is small utility to find that connection is secured or not:<\/p>\n<pre class=\"brush: csharp; title: ; notranslate\" title=\"\">\n        \/\/\/ &lt;summary&gt;\n        \/\/\/ Gets if the url is secure or not\n        \/\/\/ &lt;\/summary&gt;\n        private bool isSecureURL\n        {\n            get\n            {\n                \/\/ Check the request\n                if (HttpContext.Current.Request.IsSecureConnection ||\n                    (HttpContext.Current.Request.\n                     ServerVariables&#x5B;&quot;HTTPS&quot;] != null &amp;&amp;\n                    HttpContext.Current.Request.\n                    ServerVariables&#x5B;&quot;HTTPS&quot;].ToLower() == &quot;on&quot;))\n                    return true;\n                if ((!string.IsNullOrEmpty(HttpContext.Current.Request.\n                    ServerVariables&#x5B;&quot;HTTP_HOST&quot;].ToString())) &amp;&amp;\n                    (HttpContext.Current.Request.\n                      ServerVariables&#x5B;&quot;HTTP_HOST&quot;].ToString().\n                      EndsWith(&quot;443&quot;)))\n                    return true;\n                \/\/ url is not secure:\n                return false;\n            }\n        }\n<\/pre>\n<p>In code check whether above property returns true or false. if its true then use <strong>https <\/strong>or use <strong>http <\/strong>protocol.<\/p>\n<p>In my application, the above code behaves unexpectedly.<br \/>\nin case of http protocol, every thing is fine; but in case of https protocol, the base path of website repeats like below line.<\/p>\n<p><strong>https:\/\/yourdomainhttps:\/\/yourdomain\/jsfolder\/jsfilename<\/strong><\/p>\n<p><strong><\/strong><br \/>\nSo, the another and simplest approach i have used and which is working great is the <strong>ResolveUrl <\/strong>method of the page.<br \/>\nso the above code snap can easily written like :<br \/>\n<strong>Page.ResolveUrl(&#8220;~\/jsfolderName\/jsfileName.ja&#8221;);<\/strong><br \/>\nThe advantage of above approach is that no need to find the host name or protocol, every thing is taken care.<br \/>\nWhile reading, i found<a href=\"http:\/\/www.codeproject.com\/KB\/aspnet\/resolveurl.aspx\" target=\"_blank\"> one good article on using this approach and its advantage at this site<\/a>.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Utility program to find that current connection is secured or not, connection is http or https. Create dynamic URL in ASP.NET. Example of Page.ResolveUrl() in ASP.Net.<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"advanced_seo_description":"","jetpack_seo_html_title":"","jetpack_seo_noindex":false,"jetpack_post_was_ever_published":false,"_jetpack_newsletter_access":"","_jetpack_dont_email_post_to_subs":false,"_jetpack_newsletter_tier_id":0,"_jetpack_memberships_contains_paywalled_content":false,"jz_research_post":"","_jetpack_memberships_contains_paid_content":false,"footnotes":""},"categories":[21],"tags":[43],"class_list":["post-982","post","type-post","status-publish","format-standard","hentry","category-net","tag-asp-net"],"jetpack_featured_media_url":"","jetpack_sharing_enabled":true,"jetpack-related-posts":[{"id":2395,"url":"https:\/\/www.jitendrazaa.com\/blog\/others\/tips\/asp-net-page-lifecycle\/","url_meta":{"origin":982,"position":0},"title":"ASP.NET Page Lifecycle","author":"Jitendra","date":"August 24, 2011","format":false,"excerpt":"Tutorial and sample source code on ASP.NET Page Lifecycle","rel":"","context":"In &quot;ASP.NET&quot;","block_context":{"text":"ASP.NET","link":"https:\/\/www.jitendrazaa.com\/blog\/category\/microsoft\/net\/"},"img":{"alt_text":"Control hierarchy after page_init() event in asp.net page life cycle","src":"https:\/\/i0.wp.com\/jitendrazaa.com\/blog\/wp-content\/uploads\/2011\/08\/Control-hierarchy-after-page_init-event-in-asp.net-page-life-cycle.png?resize=350%2C200&ssl=1","width":350,"height":200},"classes":[]},{"id":2380,"url":"https:\/\/www.jitendrazaa.com\/blog\/microsoft\/net\/default-submit-button-in-aspx-or-master-page\/","url_meta":{"origin":982,"position":1},"title":"Default Submit button in ASPX or Master Page","author":"Jitendra","date":"August 12, 2011","format":false,"excerpt":"How to handle the default submit button in aspx page having contentplaceholder that is master page","rel":"","context":"In &quot;ASP.NET&quot;","block_context":{"text":"ASP.NET","link":"https:\/\/www.jitendrazaa.com\/blog\/category\/microsoft\/net\/"},"img":{"alt_text":"","src":"","width":0,"height":0},"classes":[]},{"id":1349,"url":"https:\/\/www.jitendrazaa.com\/blog\/microsoft\/csharp\/what-is-managed-and-unmanaged-code\/","url_meta":{"origin":982,"position":2},"title":"What is managed and unmanaged code","author":"Jitendra","date":"November 18, 2010","format":false,"excerpt":"What is managed and unmanaged code in .Net technology.","rel":"","context":"In &quot;c#&quot;","block_context":{"text":"c#","link":"https:\/\/www.jitendrazaa.com\/blog\/category\/microsoft\/csharp\/"},"img":{"alt_text":"","src":"","width":0,"height":0},"classes":[]},{"id":827,"url":"https:\/\/www.jitendrazaa.com\/blog\/webtech\/add-css-programatically-in-asp-net\/","url_meta":{"origin":982,"position":3},"title":"Add CSS Programatically in ASP.NET","author":"Jitendra","date":"September 3, 2010","format":false,"excerpt":"Tutorial on adding the CSS file dynamically in ASP.NET and C# \/ VB.Net","rel":"","context":"In &quot;ASP.NET&quot;","block_context":{"text":"ASP.NET","link":"https:\/\/www.jitendrazaa.com\/blog\/category\/microsoft\/net\/"},"img":{"alt_text":"","src":"","width":0,"height":0},"classes":[]},{"id":258,"url":"https:\/\/www.jitendrazaa.com\/blog\/microsoft\/net\/tracing-asp-net-website\/","url_meta":{"origin":982,"position":4},"title":"Tracing ASP.NET Website","author":"Jitendra","date":"June 28, 2010","format":false,"excerpt":"Demonstrates that how to Trace ASP.NET web applications","rel":"","context":"In &quot;ASP.NET&quot;","block_context":{"text":"ASP.NET","link":"https:\/\/www.jitendrazaa.com\/blog\/category\/microsoft\/net\/"},"img":{"alt_text":"ASP.NET Tracing Output","src":"https:\/\/i0.wp.com\/jitendrazaa.com\/blog\/wp-content\/uploads\/2010\/05\/Tracing-FirstPage1.jpg?resize=350%2C200&ssl=1","width":350,"height":200},"classes":[]},{"id":15,"url":"https:\/\/www.jitendrazaa.com\/blog\/microsoft\/net\/app_offline-htm-in-asp-net\/","url_meta":{"origin":982,"position":5},"title":"App_Offline.htm in ASP.NET","author":"Jitendra","date":"May 5, 2010","format":false,"excerpt":"If you place a file with the name App_Offline.htm in the root of a web application directory, ASP.NET 2.0 will shut-down the application, unload the application domain from the server, and stop processing any new incoming requests for that application. ASP.NET will also then respond to all requests for dynamic\u2026","rel":"","context":"In &quot;ASP.NET&quot;","block_context":{"text":"ASP.NET","link":"https:\/\/www.jitendrazaa.com\/blog\/category\/microsoft\/net\/"},"img":{"alt_text":"","src":"","width":0,"height":0},"classes":[]}],"jetpack_likes_enabled":true,"_links":{"self":[{"href":"https:\/\/www.jitendrazaa.com\/blog\/wp-json\/wp\/v2\/posts\/982","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.jitendrazaa.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.jitendrazaa.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.jitendrazaa.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.jitendrazaa.com\/blog\/wp-json\/wp\/v2\/comments?post=982"}],"version-history":[{"count":0,"href":"https:\/\/www.jitendrazaa.com\/blog\/wp-json\/wp\/v2\/posts\/982\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.jitendrazaa.com\/blog\/wp-json\/wp\/v2\/media?parent=982"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.jitendrazaa.com\/blog\/wp-json\/wp\/v2\/categories?post=982"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.jitendrazaa.com\/blog\/wp-json\/wp\/v2\/tags?post=982"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}