zoukankan      html  css  js  c++  java
  • .net设置浏览器缓存和跨域的几种方法

    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>
  • 相关阅读:
    SVN 使用锁实现独占式签出
    浏览器console中加入jquery方便调试
    nuget安装说明
    sql server 索引优化
    Windwos Server 2016 远程桌面授权
    tomcat的安装与配置
    业务监控
    敏捷话管理团队
    一键搞定多服务器的更新
    迁移历史sql数据
  • 原文地址:https://www.cnblogs.com/CeleryCabbage/p/6508798.html
Copyright © 2011-2022 走看看