public class CORSAttribute : ActionFilterAttribute { public string Domains { get; set; } public CORSAttribute( string domains) { Domains = domains; } public override void OnResultExecuted(ResultExecutedContext filterContext) { filterContext.HttpContext.Response.AppendHeader("Access-Control-Allow-Origin", Domains); filterContext.HttpContext.Response.AppendHeader("Access-Control-Allow-Headers", "Origin, X-Requested-With, Content-Type, Accept"); base.OnResultExecuted(filterContext); } }
[CORS("http://localhost:2339")] public ActionResult Country(string keyword = "", string Lang = "zh") { var json =xml2json(req.GetResponse(), "CountryDetails", "Name"); return Content(json, "text/json", Encoding.UTF8); }