简体   繁体   中英

Url.Content type method in class library code

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)?

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

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

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

    return appRootFolder;
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM