System.Web.Http.Cors two ways to configure Cross-domain access

Source: Internet
Author: User

System.Web.Http.Cors two ways to configure Cross-domain access

Using System.Web.Http.Cors to configure Cross-domain access, many great gods have published a lot of articles, I do not describe in detail, as a small white I only say about their use of experience. There are two ways to configure Cross-domain information using System.Web.Http.Cors in Webapi .
One is to configure the following code in the App_Start.WebApiConfig.cs register, which will work in all Webapi Controllers.

Using System;Using System.Collections.Generic;Using system.linq;Using System.Web.Http;Using System.web.http.cors;namespace ydtg. service{PublicStaticclass webapiconfig {public  Static void register (httpconfiguration config) {//web API Configuration and service //web API Route Config. Maphttpattributeroutes (); Config. Routes.maphttproute (name:  "defaultapi", routetemplate:  "api/{ controller}/{action}/{id} ", defaults: new {id = routeparameter.optional}); //this is the focus, read the Cross-domain address from the appsettings node of the configuration file var cors = new enablecorsattribute (configurationmanager.appsettings[ "*"); Config. Enablecors (cors); } }}
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21st
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21st
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29

Configure the file as follows, Be sure to add the HTTP

<add key="origins" value="http://localhost:9012,http://192.168.1.108:9012" />
    • 1
    • 1

  The second way is set in each Webapicontroller class, which is the code for each controller personalization configuration, as Follows.

using System; using System.Collections.Generic; using system.linq; using system.web; using System.Web.Http.Cors; using system.web.mvc;namespace service.controllers{[enablecors (  "*")] public class HomeController: Controller {public actionresult index () {viewbag.title =  "Home page"; return View (); }} 
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21st
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21st
Precautions
    1. Enablecors a total of three parameters were origins, headers and METHODS. Origins configuration allows access to the domain name, multiple domain names can be separated by commas, the domain name must be complete, if the IP address to add "http", only the use of IP will be invalidated. The headers parameter configures the supported Resources. The parameter methods configures the supported methods, get, post, put, and so On. If you allow any domain name, any resources, any method to access your own webapi, then all three parameters will use an asterisk "*" .
    2. "enablecors (" http://localhost:9012,http://192.168.1.108:9012 "," "," ")" in the configuration if there is an error, do not complain, instead, you directly prohibit access to resources that do not appear in the configuration Table.
    3. If you use the first method, you can read the list of sites from the configuration file, and if you use the second method, all parameters can only use Constants.

http://blog.csdn.net/chaoyangzhixue/article/details/52251322

Will get cors access deny, that is, cross-domain access is Denied.

For C # Do the following configuration to allow Cross-domain access to resources:

<system.webServer>
...
<customHeaders>
<add name= "access-control-allow-origin" value= "*"/>
<add name= "access-control-allow-headers" value= "Origin, x-requested-with, content-type"/>
<add name= "access-control-allow-methods" value= "put,get,post,delete,options"/>
</customHeaders>
</system.webServer>

For Nodejs do the following configuration to allow Cross-domain access to resources:

Setting Cors Cross-domain Access
App.all (' * ', function (req, res, Next) {
Res.header ("access-control-allow-origin", "*");
Res.header ("access-control-allow-headers", "x-requested-with, accept, origin, content-type");
Res.header ("access-control-allow-methods", "put,post,get,delete,options");
Res.header ("x-powered-by", ' 3.2.1 ')
Res.header ("content-type", "application/json;charset=utf-8");
Next ();
});

access-control-allow-origin:* indicates that any domain is allowed to initiate a request, and if only specific domain access is allowed, the access-control-allow-origin:xxx is set to a specific domain Name.

preflighted Requests (pre-inspection Request)

Preflighted Requests is a transparent server authentication mechanism in Cors. The preflight request first needs to send an HTTP OPTIONS request header to the resource of another domain name in order to determine whether the actual sent request is Secure.

The following 2 scenarios require a preflight:

1, simple request, such as the use of Content-type for Application/xml or Text/xml POST request;

2, set the custom head, such as x-json, X-mengxianhui and so On.

http://blog.csdn.net/chaoyangzhixue/article/details/52251322

System.Web.Http.Cors two ways to configure Cross-domain access

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.