简体   繁体   English

类库代码中的Url.Content类型方法

[英]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)? 对于类库是否有相应的('独立于Web的'C#代码)?

This little function will get the application root folder, eg '/' or '/sub-folder/' : 这个小函数将获取应用程序根文件夹,例如'/''/sub-folder/'

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

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

    return appRootFolder;
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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