Add Web API support to existing Asp.Net MVC application

Share the joy
  •  
  •  
  •  
  •  
  •  
  •  
  •  

TXTextControl.Web.MVC is using the ASP.NET Web API framework to synchronize the edited document with the server in order to render the output in a true WYSIWYG manner.

From Microsoft MSDN:

ASP.NET Web API is a framework that makes it easy to build HTTP services that reach a broad range of clients, including browsers and mobile devices. ASP.NET Web API is an ideal platform for building RESTful applications on the .NET Framework.

In our MVC tutorial, we describe how to create a new ASP.NET MVC project with Web API support. But very often, TX Text Control is added to existing MVC projects. This tutorial shows how to add ASP.NET Web API support to an existing MVC project.

  1. Click Manage NuGet Packages… from the Project main menu.
  2. Set the Package source to nuget.org and search for Microsoft.AspNet.WebApi. Select the package Microsoft.AspNet.WebApi and click on Install. Accept the license for all 4 packages by clicking I Accept.

    visualstudio_nuget_webapi

  3. In the Solution Explorer, select the folder App_Start and choose Add New Item… from the Projectmain menu. Select Class as the new item and name it WebApiConfig.cs. Confirm with Add.Add the following code to the newly created config file and import the namespace System.Web.Http:
    using System.Web.Http;
    public class WebApiConfig
    {
    public static void Register(HttpConfiguration configuration)
    {
    configuration.Routes.MapHttpRoute(“API Default”, “api/{controller}/{id}”,
    new { id = RouteParameter.Optional });
    }
    }
    view rawHosted by GitHub
  4. Import the namespace System.Web.Http in Global.asax.cs.
  5. In the file Global.asax.cs, call GlobalConfiguration.Configure(WebApiConfig.Register); in MvcApplication.Application_Start() before registering the default web application route:
    protected void Application_Start()
    {
    GlobalConfiguration.Configure(WebApiConfig.Register);
    AreaRegistration.RegisterAllAreas();
    RouteConfig.RegisterRoutes(RouteTable.Routes);
    }
    view rawHosted by GitHub

That’s it – you added Web API support to your ASP.NET MVC project. Now, you can use the TXTextControl.Web.MVC HtmlHelper in your web application.


Share the joy
  •  
  •  
  •  
  •  
  •  
  •  
  •  

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.