Skip to main content

Making Cross Enable for webAPI1

Hi All,

      I was working on webAPI1 when ever i tried to access that API from other domain it was not working.


To fix that issue we have created new class called CrossHandler.cs


Below is the code which is present in the CrossHandler.cs

 public class CorsHandler : DelegatingHandler
    {
        const string Origin = "Origin";
        const string AccessControlRequestMethod = "Access-Control-Request-Method";
        const string AccessControlRequestHeaders = "Access-Control-Request-Headers";
        const string AccessControlAllowOrigin = "Access-Control-Allow-Origin";
        const string AccessControlAllowMethods = "Access-Control-Allow-Methods";
        const string AccessControlAllowHeaders = "Access-Control-Allow-Headers";

        protected override Task<HttpResponseMessage> SendAsync(HttpRequestMessage request, CancellationToken cancellationToken)
        {
            bool isCorsRequest = request.Headers.Contains(Origin);
            bool isPreflightRequest = request.Method == HttpMethod.Options;
            if (isCorsRequest)
            {
                if (isPreflightRequest)
                {
                    return Task.Factory.StartNew<HttpResponseMessage>(() =>
                    {
                        HttpResponseMessage response = new HttpResponseMessage(HttpStatusCode.OK);
                        response.Headers.Add(AccessControlAllowOrigin, request.Headers.GetValues(Origin).First());

                        string accessControlRequestMethod = request.Headers.GetValues(AccessControlRequestMethod).FirstOrDefault();
                        if (accessControlRequestMethod != null)
                        {
                            response.Headers.Add(AccessControlAllowMethods, accessControlRequestMethod);
                        }

                        string requestedHeaders = string.Join(", ", request.Headers.GetValues(AccessControlRequestHeaders));
                        if (!string.IsNullOrEmpty(requestedHeaders))
                        {
                            response.Headers.Add(AccessControlAllowHeaders, requestedHeaders);
                        }

                        return response;
                    }, cancellationToken);
                }
                else
                {
                    return base.SendAsync(request, cancellationToken).ContinueWith<HttpResponseMessage>(t =>
                    {
                        HttpResponseMessage resp = t.Result;
                        resp.Headers.Add(AccessControlAllowOrigin, request.Headers.GetValues(Origin).First());
                        return resp;
                    });
                }
            }
            else
            {
                return base.SendAsync(request, cancellationToken);
            }
        }
    }



And then in Global.asax.cs file add this below code

Now everything got set from the API Point of view.


When it come to js file for Post Method send the object in $.param format.

example: data:$.param({ 'EnquiryId': fee.EnquiryId, 'Action': fee.Action });

and Header as headers: { 'Content-Type': 'application/x-www-form-urlencoded' }

this one worked well for me on Post Method.

Earn free bitcoin

Comments

Post a Comment

Popular posts from this blog

Ionic 3 Payment gateway integration(HDFC)

Ionic payment gateway provides a payment transaction by the transfer of information between mobile application and the acquiring bank. Below are the steps followed to integrate bank payment gateway with ionic3 application:- Step1:- Install the Cordova and Ionic Native plugins $ ionic cordova plugin add cordova-plugin-inappbrowser $ npm install –save @ionic-native/in-app-browser Step2:- Get the hash key (provides added security to the payment) from server Step3:-Create html page in WWW folder(redirect.html). This html page should contain an empty form with an id. We use this html page to submit to the payment gateway. Step4:-From form, load string to html tags, it should be in the format of how we submit to the payment gateway.  Step5:-Import inappbrowser  to app.module.ts import { InAppBrowser } from '@ionic-native/in-app-browser' ; and include it in @NgModule’s providers array. Step6:-Import inappbrowser in checkout page and add this in const

Ionic 3 Sqllite connection setup

It is too simple thing to set up the local db connection for local storage in ionic. But why am i writing blog for simple thing? Because we make complex with the simple thing only! i myself made complex with this simple thing. To setup the environment for sqllite please fallow  here . Document which will be updated as and when the new version of the Ionic get published.  Document showed the following steps when i referred it. Install the Cordova and Ionic Native plugins: $ ionic cordova plugin add cordova-sqlite-storage $ npm install --save @ionic-native/sqlite Usage import { SQLite, SQLiteObject } from '@ionic-native/sqlite' ; constructor (private sqlite: SQLite) { } ... this .sqlite.create({ name: 'data.db' , location: 'default' }) .then((db: SQLiteObject) => { db.executeSql( 'create table danceMoves(name VARCHAR(32))' , {}) .then(() => console .log( 'Executed SQL' )) .catch(e => console .log(