ASP.NET Web API 支持 CORS
2012-11-29 23:01
387 查看
Cross-Origin Resource Sharing (CORS) 是W3C草案拟定的浏览器与服务端如何进行跨域请求的方式,其原理是用自定义HTTP头来让浏览器和SERVER决定request、response的成功或失败。目前几乎所有浏览器都已经支持了(Internet Explorer 8+, Firefox 3.5+, Safari 4+, and Chrome)。
使用一个GET或POST发送一个未自定义头的请求时,会额外添加一个叫做Origin的头,其包含了请求页面的协议、域名和端口,后台可以根据这些信息很轻松的判断出是否要给出正确的response,就像下面这样
如果server允许这个请求,则会得到一个HEADER包含Access-Control-Allow-Origin的响应,而Access-Control-Allow-Origin的值则为刚刚Origin的值或*(公开资源),比如:
如果没有这个HEADER或者HEADER不匹配,浏览器则会拒绝这个请求,匹配上了就会成功,但不管是否成功,这个请求都不会带有任何cookie信息。
之前提到的所有浏览器都支持这种简单的请求方式。Firefox 3.5+, Safari 4+和 Chrome使用
ASP.NET Web API 支持 CORS 支持的代码参见 http://code.msdn.microsoft.com/Implementing-CORS-support-418970ee,也可以通过Nuget 获取WebAPIContrib
PM> Install-Package WebAPIContrib
然后给你的Web API的Action 打上标签[EnableCors],例如下面的代码
// NOTE: Maybe it is not a good idea to expose the 'model' in the service/API.
// Consider using use-case-based DTOs (http://davybrion.com/blog/2012/02/dtos-should-transfer-data-not-entities).
public class ContactsController : ApiController
{
private readonly IContactRepository repository;
public ContactsController(IContactRepository repository)
{
this.repository = repository;
}
[EnableCors]
public IQueryable<Contact> Get()
{
return repository.GetAll().AsQueryable();
}
[EnableCors]
public HttpResponseMessage Get(int id, HttpRequestMessage request)
{
var contact = repository.Get(id);
if (contact == null)
{
var response = new HttpResponseMessage(HttpStatusCode.NotFound)
{
Content = new StringContent("Contact not found.")
};
throw new HttpResponseException(response);
}
var contactResponse = request.CreateResponse(HttpStatusCode.OK, contact);
contactResponse.Content.Headers.Expires = new DateTimeOffset(DateTime.Now.AddSeconds(300));
return contactResponse;
}
参考:
http://www.nczonline.net/blog/2010/05/25/cross-domain-ajax-with-cross-origin-resource-sharing/
Using CORS to access ASP.NET services across domains
Cross-Origin Resource Sharing (CORS) and WCF
在asp.net web api中动态修改action的名字
ASP.NET Web API: CORS支持和基于属性的路由改进
Using Cross Origin Resource Sharing (CORS) in ASP.NET Web API
代码下载:
使用一个GET或POST发送一个未自定义头的请求时,会额外添加一个叫做Origin的头,其包含了请求页面的协议、域名和端口,后台可以根据这些信息很轻松的判断出是否要给出正确的response,就像下面这样
Origin: http://www.cnblogs.com
如果server允许这个请求,则会得到一个HEADER包含Access-Control-Allow-Origin的响应,而Access-Control-Allow-Origin的值则为刚刚Origin的值或*(公开资源),比如:
Access-Control-Allow-Origin: http://www.cnblogs.com
如果没有这个HEADER或者HEADER不匹配,浏览器则会拒绝这个请求,匹配上了就会成功,但不管是否成功,这个请求都不会带有任何cookie信息。
之前提到的所有浏览器都支持这种简单的请求方式。Firefox 3.5+, Safari 4+和 Chrome使用
XMLHttpRequest对象来实现,这跟普通的请求是一样的,但当浏览器发现请求的是跨域的资源时,CORS模式会自动被触发,而不需要额外的代码来处理.
ASP.NET Web API 支持 CORS 支持的代码参见 http://code.msdn.microsoft.com/Implementing-CORS-support-418970ee,也可以通过Nuget 获取WebAPIContrib
PM> Install-Package WebAPIContrib
然后给你的Web API的Action 打上标签[EnableCors],例如下面的代码
// NOTE: Maybe it is not a good idea to expose the 'model' in the service/API.
// Consider using use-case-based DTOs (http://davybrion.com/blog/2012/02/dtos-should-transfer-data-not-entities).
public class ContactsController : ApiController
{
private readonly IContactRepository repository;
public ContactsController(IContactRepository repository)
{
this.repository = repository;
}
[EnableCors]
public IQueryable<Contact> Get()
{
return repository.GetAll().AsQueryable();
}
[EnableCors]
public HttpResponseMessage Get(int id, HttpRequestMessage request)
{
var contact = repository.Get(id);
if (contact == null)
{
var response = new HttpResponseMessage(HttpStatusCode.NotFound)
{
Content = new StringContent("Contact not found.")
};
throw new HttpResponseException(response);
}
var contactResponse = request.CreateResponse(HttpStatusCode.OK, contact);
contactResponse.Content.Headers.Expires = new DateTimeOffset(DateTime.Now.AddSeconds(300));
return contactResponse;
}
参考:
http://www.nczonline.net/blog/2010/05/25/cross-domain-ajax-with-cross-origin-resource-sharing/
Using CORS to access ASP.NET services across domains
Cross-Origin Resource Sharing (CORS) and WCF
在asp.net web api中动态修改action的名字
ASP.NET Web API: CORS支持和基于属性的路由改进
Using Cross Origin Resource Sharing (CORS) in ASP.NET Web API
代码下载:
相关文章推荐
- [CORS:跨域资源共享] ASP.NET Web API自身对CORS的支持:从实例开始
- 通过扩展让ASP.NET Web API支持W3C的CORS规范
- ASP.NET Web API 2 对 CORS 的支持
- [CORS:跨域资源共享] 通过扩展让ASP.NET Web API支持JSONP
- [CORS:跨域资源共享] ASP.NET Web API自身对CORS的支持: EnableCorsAttribute特性背后的故事
- 通过微软的cors类库,让ASP.NET Web API 支持 CORS
- ASP.NET Web API自身对CORS的支持:从实例开始
- ASP.NET Web API 支持 CORS
- [CORS:跨域资源共享] ASP.NET Web API自身对CORS的支持: CORS授权检验的实施
- 支持Ajax跨域访问ASP.NET Web Api 2(Cors)的示例教程
- ASP.NET Web API自身对CORS的支持: CORS授权检验的实施
- 支持Ajax跨域访问ASP.NET Web Api 2(Cors)的示例教程
- [CORS:跨域资源共享] 通过扩展让ASP.NET Web API支持W3C的CORS规范
- ASP.NET Web API自身对CORS的支持:从实例开始
- 支持Ajax跨域访问ASP.NET Web Api 2(Cors)的示例
- ASP.NET Web API自身对CORS的支持: EnableCorsAttribute特性背后的故事
- 使用ASP.NET Web API自带的类库实现对CORS的支持(在开发中使用这种方式)(转载)
- ASP.NET Web API自身对CORS的支持: EnableCorsAttribute特性背后的故事
- ASP.NET Web API自身对CORS的支持: EnableCorsAttribute特性背后的故事
- ASP.NET Web API自身对CORS的支持: CORS授权检验的实施