Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I get the base URL of My Web Application?

Tags:

c#

iis-7

Is it possible to get the base URL of a web site hosted in IIS 7 by using Microsoft.Web.Administration.ServerManager?

In the simplest scenario, this would be:

http://localhost

but I need to get it programmatically.

If I can't use ServerManager what is the best alternative?

like image 853
Joe.Net Avatar asked Apr 10 '13 11:04

Joe.Net


People also ask

How do I find my base URL?

To find the base URL of your website, go to the site's front page. What you see in the address bar on your site's front page is the base URL of your website.

How do I find the base URL in web API?

API Host and Base URL. REST APIs have a base URL to which the endpoint paths are appended. The base URL is defined by schemes , host and basePath on the root level of the API specification. All API paths are relative to this base URL, for example, /users actually means <scheme>://<host>/<basePath>/users .

How do I find the URL for an app?

It is straightforward to get the package name of an Android app: Go to the Play store, find the app and view its page. Copy the URL from the browser address bar. Make a note of this as the "Android Fallback URL."

What is base URL in API?

The base path is the initial URL segment of the API, and does not include the host name or any additional segments for paths or operations. It is shared by all operations in the API.


2 Answers

For those interested in the usage of Microsoft.Web.Administration.ServerManager, here's some code. Consider that an IIS application my have more than one binding, resulting in more than one URL per web application.

var siteName = "Default Web Site";
var appPath = "MyWebApplication";

var serverManager = new ServerManager();
var site = serverManager.Sites[siteName];
appPath = appPath.StartsWith("/") ? appPath : "/" + appPath;
var app = site.Applications[appPath];

var urls = new List<string>();

foreach (var binding in site.Bindings)
{
    var sb = new StringBuilder();
    sb.Append(binding.Protocol);
    sb.Append("://");
    if (!string.IsNullOrWhiteSpace(binding.Host))
    {
        sb.Append(binding.Host);
    }
    else
    {
        if (Equals(binding.EndPoint.Address, IPAddress.Any))
        {
            sb.Append("localhost");
        }
        else
        {
            sb.Append(binding.EndPoint.Address);
        }
    }

    if (binding.EndPoint.Port != 80)
    {
        sb.Append(":");
        sb.Append(binding.EndPoint.Port);
    }

    sb.Append(app.Path);
    urls.Add(sb.ToString());
}
like image 72
Munir Husseini Avatar answered Oct 28 '22 00:10

Munir Husseini


You can use string baseURL = HttpContext.Current.Request.Url.Host.

like image 26
A J Avatar answered Oct 28 '22 00:10

A J