代码之家  ›  专栏  ›  技术社区  ›  Rob

后台进程的基URI

  •  1
  • Rob  · 技术社区  · 15 年前

    是否可以在线程中运行时获取当前Web应用程序的基URI?

    请记住httpcontext.current将为空,因此没有要使用的请求对象。

    目标:能够从线程向Web应用程序中的不同URL发出Web请求,以获得呈现的输出。

    2 回复  |  直到 15 年前
        1
  •  1
  •   madaboutcode    15 年前

    有几种方法可以做到这一点:

    1. 假设您可以对域使用localhost,请使用 "http://localhost" + HttpRuntime.AppDomainAppVirtualPath
    2. 如果您需要准确的域名,并确定应用程序是否使用HTTPS和所有这些花哨的东西,那么您需要访问httpRequest本身。在这种情况下,在创建线程本身的方法中创建基URL,并将其作为参数传递给线程。
        string baseUrl = Request.Url.Scheme + "://" + Request.Url.Authority 
                          + Request.ApplicationPath.TrimEnd('/') + "/";
        ParameterizedThreadStart worker= new ParameterizedThreadStart(MyWorkerMethod);
        new Thread(worker).Start(baseUrl);
    
        2
  •  0
  •   Pent Ploompuu    15 年前

    您可以从IIS ADSI提供程序获取域和协议部分,但是由于有许多绑定,您必须自己选择一个绑定:

    protected void ListServerBindings()
    {
        var ls = new List<string>();
        var ss = HttpRuntime.AppDomainAppId.Split('/');
        var e = new DirectoryEntry("IIS://localhost/" + ss[2] + "/" + ss[3]);
        foreach(string s in e.Properties["ServerBindings"])
        {
            ss = s.Split(':');
            ls.Add("http://" + (ss[2].Length == 0 ? "localhost" : ss[2]) + (ss[1] == "80" ? null : ":" + ss[1]) + HttpRuntime.AppDomainAppVirtualPath);
        }
        foreach(string s in e.Properties["SecureBindings"])
        {
            ss = s.Split(':');
            ls.Add("https://" + (ss[2].Length == 0 ? "localhost" : ss[2]) + (ss[1] == "443" ? null : ":" + ss[1]) + HttpRuntime.AppDomainAppVirtualPath);
        }
        Response.Write(string.Join("<br />", ls.ToArray()));
    }