VS: WebAPI tips

This post will have some WebAPI tips

Last updated: 2015-02-12

Add WebAPI as standalone webapp

In Visual Studio

  • New Project – C# – Web – ASP.NET WebApp – Name: MySln.WebAPI – OK
  • Template: Either
    • WebAPI //This also gives you MVC template for HelpPages
    • Empty + WebAPI ref //Only WebAPI
  • Do also add unit tests
  • Goto \MySln.WebAPI\Controllers\ValuesController.cs to see sample methods
  • Goto \MySln.WebAPI.Tests\Controllers\ValuesControllerTest.cs to see sample unit tests
  • You can also test the method with Chrome PostMan, Fiddler or just from a browser with url http://localhost:9286/api/Values where 9286 is your WebAPI web


Include standalone WebAPI webapp in existing MVC web

You can simultaneously use the above MySln.WebAPI project in your existing MySln.SomeMvcWeb webapp with few steps.

  • In Package Manager
    • PM> Install-Package Microsoft.AspNet.WebApi -ProjectName MySln.SomeMvcWeb
  • In MySln.SomeMvcWeb\Global.asax – Application_Start() – After AreaRegistration.RegisterAllAreas(); add the line
    • GlobalConfiguration.Configure(WebApiConfig.Register);
  • Copy MySln.WebAPI\App_Start\WebApiConfig.cs to MySln.SomeMvcWeb\App_Start\ and set its namespace accordingly
  • In MySln.SomeMvcWeb add ref to MySln.WebAPI
  • Now without writing any code you can use your WebAPI code from your SomeMvcWeb directly by browsing to http://localhost:16661/api/Values where 16661 is your SomeMvcWeb web. Cool :-)


ExceptionMessage”: “Multiple actions were found that match the request”

Calling service with


throws an exception

    "Message": "An error has occurred.",
    "ExceptionMessage": "Multiple actions were found that match the request: \r\nBoolean IsCurrentUserAuthenticated() on type MyProject.WebAPI.Controllers.UsersController\r\nMySln.Services.User.IUserInfo GetUserInfo() on type MySln.WebAPI.Controllers.UsersController",
    "ExceptionType": "System.InvalidOperationException",
    "StackTrace": "   at System.Web.Http.Controllers.ApiControllerActionSelector.ActionSelectorCacheItem.SelectAction(HttpControllerContext controllerContext)\r\n   at System.Web.Http.ApiController.ExecuteAsync(HttpControllerContext controllerContext, CancellationToken cancellationToken)\r\n   at System.Web.Http.Dispatcher.HttpControllerDispatcher.<SendAsync>d__0.MoveNext()"

UsersController.cs had two actions:

    public class UsersController : ApiController
        public bool IsCurrentUserAuthenticated()
        public IUserInfo GetUserInfo()

But several actions were not in WebApiConfig.cs – Do change from:

                name: "DefaultApi",
                routeTemplate: "api/{controller}/{id}",
                defaults: new { id = RouteParameter.Optional }

Do change to

                name: "DefaultApi",
                routeTemplate: "api/{controller}/{action}/{id}",
                defaults: new { id = RouteParameter.Optional }


The end.


Leave a Reply

Please log in using one of these methods to post your comment:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )


Connecting to %s

%d bloggers like this: