How to add Web API to an existing ASP.NET MVC (5) Web Application project?

Matheus Mello
Matheus Mello
September 2, 2023
Cover Image for How to add Web API to an existing ASP.NET MVC (5) Web Application project?

How to Add Web API to an existing ASP.NET MVC (5) Web Application project?

šŸ” So, you forgot to tick the Web API checkbox when you created your shiny new MVC (5) project? Don't worry, we've got your back! In this blog post, we'll guide you through the steps to add Web API to your existing ASP.NET MVC (5) project and get it up and running. šŸ’Ŗ

The Challenge

šŸ¤” Adding Web API to an existing ASP.NET MVC (5) project can be a bit tricky, especially if you're looking for the most up-to-date steps. Many migration questions out there, but none seem to have the complete and accurate solution. 🚫

Old Answers won't work anymore

The solutions you'll find for adding Web API to an MVC 4 project might not work for MVC 5. Things have changed, my friend! So, let's focus on a solution that's specifically tailored for MVC 5. šŸ“

The Solution

šŸ’” Follow these simple steps to add Web API to your existing ASP.NET MVC (5) project:

Step 1: Install the Required NuGet Packages

Open your Package Manager Console and run the following command to install the required packages:

Install-Package Microsoft.AspNet.WebApi -Version 5.2.6

This will install the necessary Web API package to your project.

Step 2: Configure Web API Routing

Open the App_Start folder in your project and locate the WebApiConfig.cs file. Add the following code:

using System.Web.Http;

namespace YourProjectNamespace
{
    public static class WebApiConfig
    {
        public static void Register(HttpConfiguration config)
        {
            // Web API routes
            config.MapHttpAttributeRoutes();

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

Step 3: Update Global.asax

Open the Global.asax.cs file and add the following code inside the Application_Start() method:

protected void Application_Start()
{
    // Other initialization code...

    // Web API configuration
    GlobalConfiguration.Configure(WebApiConfig.Register);

    // Other initialization code...
}

Step 4: Test Your Web API

Now that the Web API is added to your project, it's time to test it. Start your project, and in your browser, navigate to http://localhost:yourport/api/{controller}.

You should see the JSON representation of your data if everything is set up correctly. šŸŽ‰

Conclusion

šŸŽ‰ Congrats! You have successfully added Web API to your existing ASP.NET MVC (5) project. Wasn't that easy-peasy? Now you can take advantage of the power of Web API in your application and build some awesome features. šŸš€

šŸ‘‰ If you have any questions or encountered any issues, feel free to leave a comment below or reach out to us. We're here to help! 😊

Take Your Tech Career to the Next Level

Our application tracking tool helps you manage your job search effectively. Stay organized, track your progress, and land your dream tech job faster.

Your Product
Product promotion

Share this article

More Articles You Might Like

Latest Articles

Cover Image for How can I echo a newline in a batch file?
batch-filenewlinewindows

How can I echo a newline in a batch file?

Published on March 20, 2060

šŸ”„ šŸ’» šŸ†’ Title: "Getting a Fresh Start: How to Echo a Newline in a Batch File" Introduction: Hey there, tech enthusiasts! Have you ever found yourself in a sticky situation with your batch file output? We've got your back! In this exciting blog post, we

Cover Image for How do I run Redis on Windows?
rediswindows

How do I run Redis on Windows?

Published on March 19, 2060

# Running Redis on Windows: Easy Solutions for Redis Enthusiasts! šŸš€ Redis is a powerful and popular in-memory data structure store that offers blazing-fast performance and versatility. However, if you're a Windows user, you might have stumbled upon the c

Cover Image for Best way to strip punctuation from a string
punctuationpythonstring

Best way to strip punctuation from a string

Published on November 1, 2057

# The Art of Stripping Punctuation: Simplifying Your Strings šŸ’„āœ‚ļø Are you tired of dealing with pesky punctuation marks that cause chaos in your strings? Have no fear, for we have a solution that will strip those buggers away and leave your texts clean an

Cover Image for Purge or recreate a Ruby on Rails database
rakeruby-on-railsruby-on-rails-3

Purge or recreate a Ruby on Rails database

Published on November 27, 2032

# Purge or Recreate a Ruby on Rails Database: A Simple Guide šŸš€ So, you have a Ruby on Rails database that's full of data, and you're now considering deleting everything and starting from scratch. Should you purge the database or recreate it? šŸ¤” Well, my