Adding headers in ASP.NET MVC 3

后端 未结 2 1877
[愿得一人]
[愿得一人] 2020-12-17 16:45

I have a basic ASP.NET MVC 3 app. I have a basic action that looks like the following:

[AcceptVerbs(HttpVerbs.Post)]
public ActionResult AddItem(string id, s         


        
相关标签:
2条回答
  • 2020-12-17 17:39
        public class HttpHeaderAttribute : ActionFilterAttribute
        {
            /// 
            /// Gets or sets the name of the HTTP Header.
            /// 
            /// The name.
            public string Name { get; set; }
    
            /// 
            /// Gets or sets the value of the HTTP Header.
            /// 
            /// The value.
            public string Value { get; set; }
    
            /// 
            /// Initializes a new instance of the  class.
            /// 
            /// The name.
            /// The value.
            public HttpHeaderAttribute(string name, string value)
            {
                Name = name;
                Value = value;
            }
    
            public override void OnResultExecuted(ResultExecutedContext filterContext)
            {
                filterContext.HttpContext.Response.AppendHeader(Name, Value);
                base.OnResultExecuted(filterContext);
            }
       }    
    

    [HttpHeader("Access-Control-Allow-Origin","*")]
        public ActionResult myaction(int id)
        {
            // ...
        }
    
    0 讨论(0)
  • 2020-12-17 17:46
    Response.AppendHeader("Access-Control-Allow-Origin", "*");
    
    0 讨论(0)
提交回复
热议问题