Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/powershell/12.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
ASP.NET核心Web Api和PowerShell调用RestMethod(方法Post)加上ResponseText/ReturnValue/ErrorMessage_Powershell_Rest_Asp.net Core_Webapi - Fatal编程技术网

ASP.NET核心Web Api和PowerShell调用RestMethod(方法Post)加上ResponseText/ReturnValue/ErrorMessage

ASP.NET核心Web Api和PowerShell调用RestMethod(方法Post)加上ResponseText/ReturnValue/ErrorMessage,powershell,rest,asp.net-core,webapi,Powershell,Rest,Asp.net Core,Webapi,我目前正在开发一个ASP.NET核心Web API(v3.1)(外加一个PowerShell脚本) 控制器 [Route("api/[controller]")] [ApiController] public class RegisterDeviceController : ControllerBase { private readonly SCMDB _context; public RegisterDeviceController(SCMDB context) {

我目前正在开发一个ASP.NET核心Web API(v3.1)(外加一个PowerShell脚本)

控制器

[Route("api/[controller]")]
[ApiController]
public class RegisterDeviceController : ControllerBase
{
    private readonly SCMDB _context;

    public RegisterDeviceController(SCMDB context)
    {
        _context = context;
    }

    // POST: api/RegisterDevice
    [HttpPost]
    public async Task<ActionResult<Device>> PostDevice(Device device)
    {
        if (device.SiteKey == null)
            return StatusCode(406,"NoSiteKeyDefined!");
        return null;
    }
}
我想弄明白的是:如何在PowerShell中获得ResponseText“NoSiteKeyDefined!”

我得到:

[DBG]: PS C:\Windows\system32>> $err.Message
The remote server returned an error: (406) Not Acceptable.
但我还需要消息(在本例中->NoSiteKeyDefined!)

它不必是406错误代码,也可以是另一个“ActionResult”或响应。我还尝试了returnbadRequest(“NoSiteKeyDefined!”),但在$response和$err对象中都没有找到这条消息。重要的是,我可以传递一条可以在PowerShell脚本中接收和处理的消息

我想弄明白的是:如何在PowerShell中获得ResponseText“NoSiteKeyDefined!”

如果使用包含WebException详细消息的
$\uException
属性从
Exception
访问,我可以重现相同的问题

获取值
'NoSiteKeyDefined!'
我们设置了,我们可以直接使用
Write Host$\ucode>访问它,如下所示

try{
    $ScmWebApiUrl = 'https://xxxx/api/RegisterDevice'

    $Body = @{
         SiteKey = $NULL
    }

    $JsonString = $Body | ConvertTo-Json

    $response = Invoke-RestMethod -Uri "$ScmWebApiUrl"  -Method Post -Body $JsonString -ContentType 'application/json'
}
catch{
    Write-Host $_
    Write-Host $_.Exception
}
测试结果


你的
$JsonString
看起来怎么样?你能在VS@Avshalom中的调试中看到它的值吗?@Avshalom:基本上,$JsonString是不相关的。唯一相关的是$response,或者可能是$\异常->这就是PowerShell从REST API.interresting查找中获取/接收的内容。我无法复制它。您使用的PowerShell版本是什么?您可以发布有关$PSVersionTable的详细信息吗?我的PSV是:5.1.18362.752。我没有使用PS.NETCore,因为脚本需要在W10本机安装上运行。
try{
    $ScmWebApiUrl = 'https://xxxx/api/RegisterDevice'

    $Body = @{
         SiteKey = $NULL
    }

    $JsonString = $Body | ConvertTo-Json

    $response = Invoke-RestMethod -Uri "$ScmWebApiUrl"  -Method Post -Body $JsonString -ContentType 'application/json'
}
catch{
    Write-Host $_
    Write-Host $_.Exception
}