相关疑难解决方法(0)

如何获取站点根URL?

我想动态获取ASP.NET应用程序的绝对根URL.这需要是以下形式的应用程序的完整根URL:http(s):// hostname(:port)/

我一直在使用这种静态方法:

public static string GetSiteRootUrl()
{
    string protocol;

    if (HttpContext.Current.Request.IsSecureConnection)
        protocol = "https";
    else
        protocol = "http";

    StringBuilder uri = new StringBuilder(protocol + "://");

    string hostname = HttpContext.Current.Request.Url.Host;

    uri.Append(hostname);

    int port = HttpContext.Current.Request.Url.Port;

    if (port != 80 && port != 443)
    {
        uri.Append(":");
        uri.Append(port.ToString());
    }

    return uri.ToString();
}
Run Code Online (Sandbox Code Playgroud)

但是,如果我没有HttpContext.Current范围怎么办?我遇到过这样的情况CacheItemRemovedCallback.

asp.net

44
推荐指数
5
解决办法
9万
查看次数

ASP.Net:在共享/静态函数中使用System.Web.UI.Control.ResolveUrl()

在Asp.Net中的共享/静态函数中使用ResolveUrl()的最佳方法是什么?我目前的VB.Net解决方案是:

Dim x As New System.Web.UI.Control
x.ResolveUrl("~/someUrl")
Run Code Online (Sandbox Code Playgroud)

或C#:

System.Web.UI.Control x = new System.Web.UI.Control();
x.ResolveUrl("~/someUrl");
Run Code Online (Sandbox Code Playgroud)

但我意识到这不是调用它的最佳方式.

c# vb.net asp.net static resolveurl

34
推荐指数
2
解决办法
2万
查看次数

asp.net网页表单中的Url.Content

我正在尝试这样做:

<a href="~/Cases/SupRequestSearch.aspx">Search request</a>
Run Code Online (Sandbox Code Playgroud)

所以我需要将~其呈现为http://myserver/app/...

在mvc我会这样做

<a href="<%=Url.Content("~/Cases/SupRequestSearch.aspx")%>>Search request</a>
Run Code Online (Sandbox Code Playgroud)

asp.net网络表单中有类似的东西吗?

asp.net asp.net-mvc webforms

24
推荐指数
3
解决办法
2万
查看次数

标签 统计

asp.net ×3

asp.net-mvc ×1

c# ×1

resolveurl ×1

static ×1

vb.net ×1

webforms ×1