Using AJAX object with ASP.NET WEB API

2

Index

Stats

10,647 visits, 26,156 views

Tools

Translations

This tutorial hasn't been translated.

License

This tutorial is licensed under CC BY 4.0. Please refer to the license text if you wish to reuse, share or remix the content contained within this tutorial.

Getting things ready... step 3 of 4

Before we start we must make some adjusts, it will make things easier in the next steps ;)

Tip: You can download a template project at Tutorial Downloads with all code and packages you'll need to start coding ;) If you like to download it, jump to Step 6.

Setting JSON as the default response format

Finally, we will tell ASP.NET to always use JSON as the response format, when a Construct game send a request for it.

1. Open the WebAPIConfig.cs file at App_Start folder

2. Add this new code in any place inside the "Register" method :

    // New code
    // Use JSON as the default response format
    config.Formatters.Remove(config.Formatters.XmlFormatter); // Disable the XML formatter

Click Save in the toolbar or press Ctrl + Shift + S to save all.

Before continue...

Now, try to build the project by clicking BUID > Build Solution. If it fails, go back and revise your code.

The final WebAPIConfig.cs

The final WebAPIConfig.cs will looks like this

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Net.Http;
    using System.Web.Http;
    using Microsoft.Owin.Security.OAuth;
    using Newtonsoft.Json.Serialization;
    using System.Web.Http.Cors;

    namespace MyGameName
    {
        public static class WebApiConfig
        {
            public static void Register(HttpConfiguration config)
            {
                // New code
                // Use JSON as the default response format
                config.Formatters.Remove(config.Formatters.XmlFormatter); // Disable the XML formatter

                // New code
                // Add it in any place inside the "Register" method
                var cors = new EnableCorsAttribute("[i]", "[/i]", "*");
                config.EnableCors(cors);

                // Web API configuration and services
                // Configure Web API to use only bearer token authentication.
                config.SuppressDefaultHostAuthentication();
                config.Filters.Add(new HostAuthenticationFilter(OAuthDefaults.AuthenticationType));

                // Web API routes
                config.MapHttpAttributeRoutes();

                config.Routes.MapHttpRoute(
                    name: "DefaultApi",
                    routeTemplate: "api/{controller}/{action/}{id}", // New route, with Action
                    defaults: new { id = RouteParameter.Optional }
                );
            }
        }
    }

Click Save in the toolbar or press Ctrl + Shift + S to save all.

Before continue...

Now, try to build the project by clicking BUID > Build Solution. If it fails, go back and revise your code.

  • 2 Comments

  • Order by
Want to leave a comment? Login or Register an account!