Note: Regardless of whether the APB Zero module is installed, you can use the cross-domain
First configure the Web API:
1. Under the Web API Project, install the package
Install-package Microsoft.AspNet.WebApi.Cors
2. Then in Webapimodule method Initialize, add the following code: (The code is best on the top of Configureswaggerui)
// set up cross-domain var New Enablecorsattribute ("*""*" "*"* "); true ; GlobalConfiguration.Configuration.EnableCors (cors);
The cross-domain of the Web API is configured above, and then we want to support cross-domain in web MVC as well. Below we give an example of the login module support cross-domain.
Then configure the Web MVC project to allow login support across domains:
1. We create a attribute class, called Acceessoriginalattribute, as follows:
usingSystem;usingSystem.Collections.Generic;usingSystem.Configuration;usingSystem.Linq;usingsystem.web;usingSYSTEM.WEB.MVC;namespacemagicqces.web.filters{ Public classAcceessoriginalattribute:actionfilterattribute, Iexceptionfilter { Public voidInternalexuteaccesscontrole (httpresponsebase response, httprequestbase request) {varHeader =Response. Headers; varRequestheader =request. Headers; Header. Set ("Access-control-allow-origin", requestheader["Origin"] ??"/ http"+ requestheader["Host"]); Header. Set ("Access-control-allow-methods","POST, GET, OPTIONS, DELETE"); Header. Set ("Access-control-max-age","3600"); Header. Set ("access-control-allow-headers","*"); Header. Set ("access-control-allow-credentials","true"); } Public Override voidonactionexecuting (ActionExecutingContext filtercontext) {Base. OnActionExecuting (Filtercontext); } Public Override voidonactionexecuted (ActionExecutedContext filtercontext) {internalexuteaccesscontrole (FilterContext.H Ttpcontext.response, filterContext.HttpContext.Request); Base. OnActionExecuted (Filtercontext); } Public Override voidonresultexecuting (ResultExecutingContext filtercontext) {Base. Onresultexecuting (Filtercontext); } Public Override voidonresultexecuted (ResultExecutedContext filtercontext) {Base. Onresultexecuted (Filtercontext); } Public voidonexception (Exceptioncontext filtercontext) {Filtercontext.result=NewJsonresult () {Data=New{succeed =0, Message =FilterContext.Exception.Message}, Jsonrequestbehavior=Jsonrequestbehavior.allowget}; Filtercontext.exceptionhandled=true; } }}
2. Then, in AccountController, in the login method, add the above attribute, as follows:
[acceessoriginal] publicasync task<jsonresult> Login (string string Password)
In this way, the login also supports cross-domain.
C # ABP allows cross-domain requests