【问题标题】:How do i enable cache in my RestAPI in c#?如何在 C# 的 Rest API 中启用缓存?
【发布时间】:2016-08-29 05:25:56
【问题描述】:

这是我的 EmployeeDetailsController.cs

namespace EmpApi.Controllers
{
    [RoutePrefix("")]

    public class EmployeeDetailsController : ApiController
    {

        [HttpGet]

        [Route("Employees")]

        public IEnumerable<Employee> Employees()
        {

        }


        [HttpGet]
        [Route("Details/{id}")]
        public IEnumerable<Details> Details(int id)
        {
        }


        [HttpGet]
        [Route("TeamInfo/{id}")]
        public IEnumerable<Team> TeamInfo(int id)
        {

        }

        [HttpGet]
        [Route("DetailsForTeam/{id}")]
        public IEnumerable<Details> DetailsForTeam(int id)
        {
            ;
        }
        [HttpPost]
        [Route("PostEmp")]
        public void PostEmp([FromBody] Employee cs)
        {

        }
        [HttpPut]
        [Route("PutEmp/{id}")]
        public void PutEmp(int id, [FromBody]Employee cs)
        {

        }
        [HttpDelete]
        [Route("DeleteEmp/{id}")]
        public void DeleteEmp(int id)
        {

        } 
    }
}

我制作了一个包含各种服务的 API。 假设我调用 api/Employees,在我调用 api/Details/12 之后,然后当我在浏览器中单击 GoBack 按钮时,不应触发 api/Employees。 如何为我的 API 启用缓存。请告诉我步骤,因为我是 WebApI 的新手。 提前谢谢..

【问题讨论】:

  • 如何点回 API 调用?请澄清。您的文字几乎听起来像是您想要启用缓存,但您询问是否要禁用它。
  • 我已经编辑了@Sami Kuhmonen 的问题

标签: c# asp.net-mvc-4 asp.net-web-api2 browser-cache


【解决方案1】:

在您的控制器声明之前添加此代码,如下所示:

[OutputCache(VaryByParam = "*", Duration = 0, NoStore = true)]
public class EmployeeDetailsController : ApiController
{
...
}

【讨论】:

    猜你喜欢
    • 2021-06-02
    • 2017-09-03
    • 2016-11-14
    • 1970-01-01
    • 2014-09-09
    • 2021-08-16
    • 2021-10-29
    • 1970-01-01
    • 2015-02-12
    相关资源
    最近更新 更多