你用Content-Type: application/json
做了請求,所以正文中的字符串被當作JSON字符串處理。並且JSON字符串必須用雙引號引起來,並且使用\
字符(specification)應該轉義特殊字符。
所以你應該發佈路徑爲"C:\\Users\\futerm\\Downloads\\test"
。
如果你不想做的字符轉義,然後再考慮做一個請求與Content-Type: text/plain
。但是,您需要修改代碼以直接從請求正文中讀取。
控制器操作。
[HttpPost]
public async Task<IActionResult> Post()
{
var directoryPath = await Request.GetRawBodyStringAsync();
//_log.Debug($"Got ScanDirectory request for directoryPath:{directoryPath}");
if (string.IsNullOrEmpty(directoryPath))
{
return NotFound("DirectoryPath is empty");
}
return Ok(directoryPath);
}
Helper方法:
public static class HttpRequestExtensions
{
/// <summary>
/// Retrieve the raw body as a string from the Request.Body stream
/// </summary>
/// <param name="request">Request instance to apply to</param>
/// <param name="encoding">Optional - Encoding, defaults to UTF8</param>
/// <returns></returns>
public static async Task<string> GetRawBodyStringAsync(this Microsoft.AspNetCore.Http.HttpRequest request, System.Text.Encoding encoding = null)
{
if (encoding == null)
encoding = System.Text.Encoding.UTF8;
using (var reader = new System.IO.StreamReader(request.Body, encoding))
return await reader.ReadToEndAsync();
}
}
以上的擴展方法主要是採取從Accepting Raw Request Body Content文章。
來源
2017-09-27 09:18:18
Set