3

I have this code in a class library:

string.Format("{0}://{1}", Current.Request.Url.Scheme, Current.Request.Url.Authority);

This works fine if the application is deployed in the root domain and not a subdomain.

I would like to adapt the above to work for a sub domain as well. In the razor code I can just use:

Url.Content("~/")

Is there an equivalent for this for class libraries ('web independent' C# code)?

3
  • Do you need to form a fully-qualified URL, e.g. 'http://www.example.com/controller/action', or an application root-relative URL, e.g. '/controller/action'?
    – Netricity
    May 22, 2013 at 12:20
  • I think I found a way. I could use: System.Web.HttpContext.Current.Request.ApplicationPath to get the 'bit' I need
    – cs0815
    May 22, 2013 at 12:23
  • Yes, and append '/' if the ApplicationPath is a sub-folder.
    – Netricity
    May 22, 2013 at 12:31

1 Answer 1

2

This little function will get the application root folder, e.g. '/' or '/sub-folder/':

string GetAppRootFolder()
{
    var appRootFolder = HttpContext.Current.Request.ApplicationPath.ToLower();

    if (!appRootFolder.EndsWith("/"))
    {
        appRootFolder += "/";
    }

    return appRootFolder;
}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.