通过PowerShell将多个查询参数传递给WebService

SKL*_*LAK 3 c# powershell

我通过PowerShell调用webservice GET方法,如下所示:

$result = Invoke-WebRequest -Uri https://localhost/MyApi/Foo/blahA/blahB?three=testing&four=2015-09-18T06:45:29.5199432Z
Run Code Online (Sandbox Code Playgroud)

但是,这给了我以下错误:

不允许使用&符(&)字符.&运营商留作将来使用; 用双引号("&")包装&符号以将其作为字符串的一部分传递.

  • CategoryInfo:ParserError:(:) [],ParentContainsErrorRecordException
  • FullyQualifiedErrorId:AmpersandNotAllowed

如果重要的是我的WebAPI代码和路由(该方法有两个必需参数,3个可选):

[HttpGet, ActionName("Foo")]
public string Foo(string one, string two, 
       string three = null, DateTime? four = null, int five = null)
{
    //do stuff
    //return some string
}
...
routes.MapHttpRoute(
           name: "Foo",
           routeTemplate: "Foo/{one}/{two}",
           defaults: new { controller = "Testing", action = "Foo" }
           );
Run Code Online (Sandbox Code Playgroud)

Tim*_*ski 7

一些事情,你应该用双引号包装你的URI:

$result = Invoke-WebRequest -Uri "https://localhost/MyApi/Foo/blahA/blahB?three=testing&four=2015-09-18T06:45:29.5199432Z"
Run Code Online (Sandbox Code Playgroud)

其次,你的网址一般看起来不对,试试吧

$result = Invoke-WebRequest -Uri "https://localhost/MyApi/Foo?one=blahA&two=blahB&three=testing&four=2015-09-18T06:45:29.5199432Z"
Run Code Online (Sandbox Code Playgroud)