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>
  • 相关阅读:
    浅谈MVP与ModelViewViewModel(MVVM)设计模式
    策略模式
    C#验证码
    如何招到烂程序员
    承载和使用WCF服务
    .NET Remoting 使用总结
    基于.Net Remoting的应用程序
    HTML5 是什么?
    关于HTTP及XMLHTTP状态代码一览
    Remoting多个信道(Chennel)的注册问题
  • 原文地址:https://www.cnblogs.com/CeleryCabbage/p/6508798.html
Copyright © 2011-2022 走看看