C# 如何从类中获取字符串

C# 如何从类中获取字符串,c#,asp.net,.net,C#,Asp.net,.net,我有Api控制器 public class JsonController : Controller { private readonly IRepositor _repositor; public JsonController(IRepositor repositor) { _repositor = repositor; } // GET: api/Json [HttpGet] public string Get()

我有Api控制器

public class JsonController : Controller
{
    private readonly IRepositor _repositor;
    public JsonController(IRepositor repositor)
    {
        _repositor = repositor;
    }
    // GET: api/Json
    [HttpGet]
    public string Get()
    {
        return Repositions.StrocTable;
    }
    // POST: api/Json
    [HttpPost]
    public void Post([FromBody]List<Json> jsons)
    {
        _repositor.DataSetTwo(jsons);
    }
}
当我第二次执行时,我得到了空字符串,我得到了第一次应该出现的字符串。 我不知道怎么称呼这个问题。
谢谢。

您需要从控制器返回Json,然后在前端JavaScript上处理它

将Get方法更改为:

[HttpGet]
public string Get()
{
    return Json(Repositions.StrocTable);
}

您应该看看web API()控制器的生命周期。简而言之:每个请求都有一个新的控制器。由于
重新定位。在执行
Get
请求时,不会设置strotable
。通过使用
static
属性(strotable),每个用户都会看到相同的值。这可能不是你想要的。谢谢你的回答
$.ajax({
    type: 'POST',
    url: '/api/Json',
    data: JSON.stringify(jsons),
    contentType: 'application/json'
}).done($(hash).empty();

$.get('/api/Json').done(function (Stroctable) {
    $('#Tables').append(Stroctable);
}););
[HttpGet]
public string Get()
{
    return Json(Repositions.StrocTable);
}