如何在ASP.NET MVC中处理“ OPTIONS方法”

如何在ASP.NET MVC中处理“ OPTIONS方法”,第1张

如何在ASP.NET MVC中处理“ OPTIONS方法

原来我必须创建一个

ActionFilterAttribute

namespace WebService.Attributes{    public class AllowCrossSiteJsonAttribute : ActionFilterAttribute    {        public override void onActionExecuting(ActionExecutingContext filterContext)        { HttpContext.Current.Response.Cache.SetCacheability(HttpCacheability.NoCache); HttpContext.Current.Response.Cache.SetNoStore(); filterContext.RequestContext.HttpContext.Response.AppendHeader("Access-Control-Allow-Origin", "*"); string rqstMethod = HttpContext.Current.Request.Headers["Access-Control-Request-Method"]; if (rqstMethod == "OPTIONS" || rqstMethod == "POST") {     filterContext.RequestContext.HttpContext.Response.AppendHeader("Access-Control-Allow-Methods", "GET, POST, OPTIONS");     filterContext.RequestContext.HttpContext.Response.AppendHeader("Access-Control-Allow-Headers", "X-Requested-With, Accept, Access-Control-Allow-Origin, Content-Type"); } base.onActionExecuting(filterContext);        }    }}


欢迎分享,转载请注明来源:内存溢出

原文地址:https://54852.com/zaji/5002107.html

(0)
打赏 微信扫一扫微信扫一扫 支付宝扫一扫支付宝扫一扫
上一篇 2022-11-14
下一篇2022-11-14

发表评论

登录后才能评论

评论列表(0条)

    保存