Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/278.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 如何动态查找我的url?httpc客户端#_C# - Fatal编程技术网

C# 如何动态查找我的url?httpc客户端#

C# 如何动态查找我的url?httpc客户端#,c#,C#,如何在启动时动态查找它而不是localhost:60792?给我指出正确的方向就足够了。但如果你有答案,请告诉我 更新谢谢你的提示。我最后的答案是 string url=HttpContext.Current.Request.url.AbsoluteUri字符串url=HttpContext.Current.Request.url.AbsoluteUri在Visual studio 2019的新更新之后,@webNoob的回答将给出一个错误 “HttpContext”不包含“Current”的定

如何在启动时动态查找它而不是localhost:60792?给我指出正确的方向就足够了。但如果你有答案,请告诉我

更新谢谢你的提示。我最后的答案是
string url=HttpContext.Current.Request.url.AbsoluteUri

字符串url=HttpContext.Current.Request.url.AbsoluteUri

在Visual studio 2019的新更新之后,@webNoob的回答将给出一个错误

“HttpContext”不包含“Current”的定义,并且找不到接受“HttpContext”类型的第一个参数的可访问扩展方法“Current”(是否缺少using指令或程序集引用?

因此,即使您使用
System.web
System.Net.Http
也会得到相同的错误

您仍然可以将以下内容用作修复:

用于获取字符串的查询

  • 用于获取查询
    var url1=HttpContext.Request.Query.Keys

  • 为了获得路径
    var url2=HttpContext.Request.Path.Value


  • 在Visual studio 2019的新更新之后,@webNoob的回答将给出一个错误

    “HttpContext”不包含“Current”的定义,并且找不到接受“HttpContext”类型的第一个参数的可访问扩展方法“Current”(是否缺少using指令或程序集引用?

    因此,即使您使用
    System.web
    System.Net.Http
    也会得到相同的错误

    您仍然可以将以下内容用作修复:

    用于获取字符串的查询

  • 用于获取查询
    var url1=HttpContext.Request.Query.Keys

  • 为了获得路径
    var url2=HttpContext.Request.Path.Value


  • 您要查找的url是什么?您有什么类型的应用程序?我正在尝试查找我的根url您正在尝试查找的url是什么?您有什么类型的应用程序?我正在尝试查找我的根url
    HttpClient client = new HttpClient(); 
    client.BaseAddress = new Uri("http://localhost:60792"); 
    
    String baseURL = string.Format(
          (
            System.Web.HttpContext.Current.Request.Url.Port != 80) ? "{0}://{1}:{2}" : "{0}://{1}",
            System.Web.HttpContext.Current.Request.Url.Scheme,
            System.Web.HttpContext.Current.Request.Url.Host,
            System.Web.HttpContext.Current.Request.Url.Port
    );