C # ABP allows cross-domain requests

Source: Internet
Author: User

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

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.