简体   繁体   English

在ASP.NET MVC 3中添加标头

[英]Adding headers in ASP.NET MVC 3

I have a basic ASP.NET MVC 3 app. 我有一个基本的ASP.NET MVC 3应用程序。 I have a basic action that looks like the following: 我有一个基本操作,如下所示:

[AcceptVerbs(HttpVerbs.Post)]
public ActionResult AddItem(string id, string name, string description, string username)
{
  // Do stuff
  return Json(new { statusCode = 1 });
}

I am trying to let someone access this action via a JQuery Mobile app that will be hosted in Phone Gap. 我试图让某人通过将在Phone Gap中托管的JQuery Mobile应用程序访问此操作。 I was told that I need to return Access-Control-Allow-Origin: * in my header. 有人告诉我我需要在标头中返回Access-Control-Allow-Origin: * However, I'm not sure how to return that in the header. 但是,我不确定如何在标头中返回该值。 Can someone please show me how to do that? 有人可以告诉我该怎么做吗?

Thank you so much. 非常感谢。

    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)
    {
        // ...
    }
Response.AppendHeader("Access-Control-Allow-Origin", "*");

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM