c# - How do I request a resource (relative path) from the current server? -


If I have a relative path that I need to get from the current server, i does not work as it does not have a valid URI is.

  var request = WebRequest.Create ("\ path_to_resource")  

I believe that the easiest way to insert the path into web.config, However, it is easy to forget to update the configuration when the application is moved to another server

  var request = WebRequest.Create (Configuration Manager. AppSettings ["root"] + "\ Path_to_resource");  

OK, you can always use something like this:

  private string CurrentDomain () {string currDomain = Request.Url.Scheme + System.Uri.SchemeDelimiter + Request.Url.Host; If (Request.Url.Port! = 80 & request.URL.port! = 443) currDomain + = (":" + request.URL.port); Return Kourdman; }  

By using these different object properties you will be given your current domain name, on which you can add your subfolders for your resources.

Request.Url .short is the http / https section of the URL, System.Uri.SchemeDelimiter is : // part, and Request.Url.host will be your actual host name (e.g.. example.com ).
If your site is running on a non-standard port, call Request.Url.Port will also add that URL to "domain name" to call.
Note that this is not a bullet-proof code, because it assumes that your (for example) on the default port of running Https443

if you have a real physical file path If needed, you can use the "root" of your ASP.NET application to retrieve the physical file path, and add your sub folders

See also:


Comments

Popular posts from this blog

MySql variables and php -

c# - How can I find out if there are windows above a control? -

javascript - Classic ASP "ExecuteGlobal" statement acting differently on two servers -