C# 将缓存的MVC操作限制为Request.IsLocal?

C# 将缓存的MVC操作限制为Request.IsLocal?,c#,asp.net-mvc,outputcache,C#,Asp.net Mvc,Outputcache,我有一个带有OutputCache的MVC操作,因为我需要缓存数据以最小化对myService的调用 [HttpGet] [OutputCache(Duration = 86400, Location = OutputCacheLocation.ServerAndClient, VaryByParam = "myVariable")] public JsonResult GetStuffData(string myVariable) { if (Request.IsLocal)

我有一个带有OutputCache的MVC操作,因为我需要缓存数据以最小化对myService的调用

[HttpGet]
[OutputCache(Duration = 86400, Location = OutputCacheLocation.ServerAndClient, VaryByParam = "myVariable")]
public JsonResult GetStuffData(string myVariable)
{
    if (Request.IsLocal)
    {
        return myService.CalculateStuff(myVariable)
    }
    else
    {
        return null;
    }
}
我希望它只能从运行它的服务器上访问,因此是Request.IsLocal

这很好,但是如果有人远程访问GetStuffData,它将返回null,null将被缓存一天。。。使特定的GetStuffData(myVariable)在一天内无用

类似地,如果首先在本地调用,则外部请求将接收缓存的本地数据

是否有办法将整个函数限制为Request.IsLocal而不仅仅是返回值?

例如,如果它是外部访问的,你只会得到404,或者找不到方法等等。但是如果它是Request.Local,你会得到缓存结果

如果没有缓存,这将运行得非常好,但我正在努力找到一种将Request.IsLocal和缓存结合起来的方法

可能相关的额外信息:

我通过C#调用GetStuffData来获取缓存的StuffData,方法是获取一个json对象,如下所示。。。(直接调用该操作从未导致缓存它,因此我切换到模拟webrequest)


您可以使用自定义授权筛选器属性,如

public class OnlyLocalRequests : AuthorizeAttribute
{
        protected override bool AuthorizeCore(HttpContextBase httpContext)
        {
            if (!httpContext.Request.IsLocal)
            {
                httpContext.Response.StatusCode = (int)HttpStatusCode.NotFound;
                return false;
            }
            return true;
        }
}
把你的行为装饰成

[HttpGet]
[OnlyLocalRequests]
[OutputCache(Duration = 86400, Location = OutputCacheLocation.ServerAndClient, VaryByParam = "myVariable")]
public JsonResult GetStuffData(string myVariable)
{}

看起来不错,但不太管用。如果我第一次从外部访问它,我得到了404(很好)。如果我在本地访问它,我会得到结果(很好)。如果我从外部访问它,我会得到缓存的结果。所以还是一个problem@mejobloggs我已经更新了
onlyCalRequests
类。非常好。不会返回404,但在我外部访问时会将我发送到登录页面,但这很好。一切正常
[HttpGet]
[OnlyLocalRequests]
[OutputCache(Duration = 86400, Location = OutputCacheLocation.ServerAndClient, VaryByParam = "myVariable")]
public JsonResult GetStuffData(string myVariable)
{}