【发布时间】:2019-08-17 10:22:48
【问题描述】:
我需要用 asp.net 编写一个 api restfull,它在正文中接收一个 json,例如:
"{
\"name\" : \"name of the file\",
\"path\" : \"path of the file\",
\"body\" : \"body of th file\"
}"
并使用来自该 json 的数据在服务器中写入一个文件。
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Hosting.Server;
using Microsoft.AspNetCore.Mvc;
using Newtonsoft.Json;
namespace tool_put.Controllers
{
[Route("api/[controller]")]
[ApiController]
public class ValuesController : ControllerBase
{
[HttpPost]
public String Post([FromBody] string value)
{
dynamic obj = JsonConvert.DeserializeObject(value);
String path = obj.path + obj.name + ".txt";
//StreamWriter Data = new StreamWriter(MapPath("~/"+path), (System.Text.Encoding)true);
// Data.WriteLine(obj.body);
return "true";
}
}
}
我在 mac 上使用了 Visual Studio,当我在 localhost 上运行它并尝试从邮递员执行 http post 时希望没有创建的文件。 请注意他的 cmets,因为这个带有 StreamWriter 对象的解决方案不起作用。 如何解决这个问题? 有没有其他方法可以在服务器上的txt文件中写入一些文本?
【问题讨论】:
标签: c# asp.net visual-studio asp.net-web-api asp.net-core