You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

95 lines
2.8 KiB
C#

1 year ago
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
1 year ago
using Microsoft.Extensions.Caching.Memory;
1 year ago
using WebMVCApi.Models;
1 year ago
using Zack.ASPNETCore;
1 year ago
namespace WebMVCApi.Controllers
{
//[controller] 当前controller 的名字 [action] 代表controller 里面方法的名字
//api/[controller] 表示当前controller 里的所有方法都被解析成 /api/Test
//api/[action] 表示当前controller 里的所有方法都被解析成 /api/GetPerson ,/api/SaveNote
[Route("api/")]
[ApiController]
public class TestController : ControllerBase
{
1 year ago
private IMemoryCache _memoryCache;
1 year ago
private IMemoryCacheHelper _memoryCacheHelper;
1 year ago
1 year ago
public TestController(IMemoryCache _memoryCache, IMemoryCacheHelper _memoryCacheHelper) {
1 year ago
this._memoryCache= _memoryCache;
1 year ago
this._memoryCacheHelper = _memoryCacheHelper;
1 year ago
}
1 year ago
[HttpGet("GetPerson")]
public Person GetPerson()
{
return new Person("zs", "13");
}
[HttpPost("SaveNote")]
public string SaveNote(SaveNoteRequest request)
{
string filename = $"{request.Title}.txt";
System.IO.File.WriteAllText(filename, request.Content);
return filename;
}
[HttpGet("GetId")]
public IActionResult GetId(int id)
{
if (id < 10)
{
return Ok(new { id = 10 });
}
else {
//throw new Exception("id 错误");
return NotFound(id);
}
}
[HttpGet("GetId2")]
public ActionResult<Object> GetId2(int id)
{
if (id < 10)
{
return Ok(new { id = 10 });
}
else
{
//throw new Exception("id 错误");
return NotFound(id);
}
}
[HttpGet("Add/{i1}/{i2}")]
public ActionResult<Object> Add(int i1, int i2)
{
return i1 + i2;
}
[HttpGet("students/school/{schoolName}/class/{classNo}")]
public ActionResult<Object> GetMessage(string schoolName,[FromRoute(Name = "classNo")] int classNom)
{
return new { Id = classNom+6, schoolName=schoolName + "扛把子", };
}
1 year ago
1 year ago
[HttpPut("UpdatePerson")]
1 year ago
public async Task<Object> UpdatePerson(int id, Person p1)
1 year ago
{
1 year ago
return _memoryCache.GetOrCreateAsync<String>("val", async (e)=>{
1 year ago
1 year ago
return $"id {id} 的用户{p1.Name}更新成功 {DateTime.Now}";
});
1 year ago
}
1 year ago
[HttpGet("test1")]
public async Task<Object> test1(long id)
{
return _memoryCacheHelper.GetOrCreateAsync<int>($"Book{id}",async (e) =>
{
return 123;
},10);
}
1 year ago
}
}