码迷,mamicode.com
首页 > Web开发 > 详细

.net设置浏览器缓存和跨域的几种方法

时间:2017-03-06 11:34:05      阅读:167      评论:0      收藏:0      [点我收藏+]

标签:oca   requested   span   tca   net   request   protocol   public   conf   

1.自定义过滤器属性
public class NoCacheAttribute : FilterAttribute, IActionFilter
{

    public void OnActionExecuted(ActionExecutedContext filterContext)
    {
        //设置浏览器缓存
        filterContext.HttpContext.Response.Cache.SetCacheability(HttpCacheability.NoCache);
        //设置接口跨域
        filterContext.HttpContext.Response.AddHeader("Access-Control-Allow-Origin","*");

    }
    public void OnActionExecuting(ActionExecutingContext filterContext)
    {
    }
}
或者
context.Response.ClearHeaders(); 
context.Response.AppendHeader("Access-Control-Allow-Origin","*"); 
string requestHeaders = context.Request.Headers["Access-Control-Request-Headers"]; 
context.Response.AppendHeader("Access-Control-Allow-Headers", 
string.IsNullOrEmpty(requestHeaders) ? "*" : requestHeaders); 
context.Response.AppendHeader("Access-Control-Allow-Methods", "POST, OPTIONS"); 
2.web.config文件里配置
<system.webServer>  
  <httpProtocol> 
    <customHeaders> 
      <add name="Access-Control-Allow-Methods" value="OPTIONS,POST,GET"/> 
      <add name="Access-Control-Allow-Headers" value="x-requested-with"/> 
      <add name="Access-Control-Allow-Origin" value="*" /> 
    </customHeaders> 
  </httpProtocol> 
</system.webServer>

 

.net设置浏览器缓存和跨域的几种方法

标签:oca   requested   span   tca   net   request   protocol   public   conf   

原文地址:http://www.cnblogs.com/weishuanbao/p/6508798.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!