C#, Cross Domain, HTML5, JavaScript, NodeJS

Visual Studio Code – A Cross Platform Code Editor built with Chromium

Microsoft has launched VSCode few weeks back for Cross Platform (Linux/Mac,Windows) and 55% download was Non-Windows. Shocking , right ?

HackerNews had a post on this and had really lot of comments/traffic on it.

Lets see the first look of Code.

Code First Look

Code First Look

Its lightweight and simple and has GIT embedded with this.

But I am not here to say that Code has been released by Microsoft. I am here to shock you little bit.

When you are on Code, Just press F12.

And lets see the below image.

Code First Look Shocked

Code First Look Shocked

#%$#@ ???? Is it Chrome Dev Tools ?

YES !!! It is!

In fact VSCode has been built with Web Technologies like HTML,CSS , JavaScript with Chromium and NodeJS . Here , Chromium is the Engine for Chrome browser by Google.

Lets get a little idea about CEF (Chromium Embedded Framework)

Tech

And they used Electron for Cross Platform . Electron helps to Build cross platform desktop apps with web technologies Formerly known as Atom Shell. Made with Love by GitHub.

Initially developed For GitHub’s Atom editor , Electron has since been used to create applications by companies like Microsoft, Facebook, Slack, and Docker.

Slack‘s new Desktop app was also built with this.

In VSCode , what we do Like adding/editing Projects/Folders/Files , all is saved in HTML5’s Client Side DBs like IndexedDB and LocalStorage. To know more about HTML5 Database , you can see my other posts on HTML5 Database here
https://shahjadatalukdar.wordpress.com/2013/09/27/using-html5-localstorage-with-angularjs/

https://shahjadatalukdar.wordpress.com/2014/01/18/websql-welcome-to-client-side-db-part1/

Lets see the below image.

HTML5 Database

HTML5 Database

Okay , lets see Where the World goes to!

Cheers!

//pagead2.googlesyndication.com/pagead/js/adsbygoogle.js

(adsbygoogle = window.adsbygoogle || []).push({});

Advertisements
Standard
ASP.Net, C#, Cross Domain, jQuery, jQuery AJAX

Using CORS in ASP.NET MVC Web API

Cross-site HTTP requests are HTTP requests for resources from a different domain than the domain of the resource making the request. For instance, a resource loaded from Domain A (http://domaina.example) such as an HTML web page, makes a request for a resource on Domain B (http://domainb.foo), such as an image, using the img element (http://domainb.foo/image.jpg). This occurs very commonly on the web today — pages load a number of resources in a cross-site manner, including CSS stylesheets, images and scripts, and other resources.

As an example –
If my Server is in http://www.shahjada.com and my client (which is hosted at http://www.shahjada-talukdar.com) calls that Server with an AJAX GET , it will give an error for Cross Domain Issue .

If the Client and Server is in the same server but in different port , It will also give Cross Domain error . Only if Client and Server are in same Address , it will work nicely.

Suppose , My ASP.net MVC WebAPI is hosted at http://www.shahjada-talukdar.com and my client is in http://www.shahjada.com , It will throw the error .
We can fix this issue by Using Cross Origin Resource Sharing (CORS) in ASP.NET Web API Server .

For that , We can create a CORS handler and add that handler to GlobalCOnfiguration’s MessageHandler .
My Cors handler is something like this :

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)
                {
                    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);
                    }

                    TaskCompletionSource<HttpResponseMessage> tcs = new TaskCompletionSource<HttpResponseMessage>();
                    tcs.SetResult(response);
                    return tcs.Task;
                }
                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);
            }
        }
    }

Now Add that to Global.asax.cs like below :

GlobalConfiguration.Configuration.MessageHandlers.Add(new CorsHandler());

Now our Server is ready to handle any Cross Domain Client’s Call .

Cheers 🙂

Standard