How do you debug MVC 4 API routes?

C#.Netasp.net MvcRestasp.net Mvc-4

C# Problem Overview


I have a WP7 game that uses RESTsharp to communicate with my MVC4 RESTful server, but I often have issues making requests that work and therefore I want to debug where it fails.

This is an example where the Constructor on my GameController is hit, but the Post method is not hit, and I don't understand why.

Client code:

public void JoinRandomGame()
{
  client = new RestClient
  {
      CookieContainer = new CookieContainer(),
      BaseUrl = "http://localhost:21688/api/",
  };
        
  client.Authenticator = GetAuth();

  RestRequest request = new RestRequest(Method.POST)
  {
      RequestFormat = DataFormat.Json,
      Resource = "game/"
            
  };

  client.PostAsync(request, (response, ds) =>
  {});
}

Server code:

    public void Post(int id)
    {
        if (ControllerContext.Request.Headers.Authorization == null)
        {
            //No auth
        }
        if (!loginManager.VerifyLogin(ControllerContext.Request.Headers.Authorization.Parameter))
        {
            //Failed login
        }

        string username;
        string password;
        LoginManager.DecodeBase64(ControllerContext.Request.Headers.Authorization.Parameter, out username, out password);
        gameManager.JoinRandomGame(username);
    }

My routes are like this

       routes.MapHttpRoute(
            name: "gameAPI",
            routeTemplate: "api/game/{gameId}",
            defaults: new
            {
                controller = "game",
                gameId = RouteParameter.Optional
            }             
        );

C# Solutions


Solution 1 - C#

Another way is to add an event handler in Global.asax.cs to pick up the incoming request and then look at the route values in the VS debugger. Override the Init method as follows:

public override void Init()
{
	base.Init();
	this.AcquireRequestState += showRouteValues;
}

protected void showRouteValues(object sender, EventArgs e)
{
	var context = HttpContext.Current;
	if (context == null)
		return;
	var routeData = RouteTable.Routes.GetRouteData(new HttpContextWrapper(context)); 
}

Then set a breakpoint in showRouteValues and look at the contents of routeData.

Keep in mind that in a Web API project, the Http routes are in WebApiConfig.cs, not RouteConfig.cs.

Solution 2 - C#

RouteDebugger is good for figuring out which routes will/will not be hit.

http://nuget.org/packages/routedebugger

Solution 3 - C#

Solution 4 - C#

There are more possibilities for testing the routes. You can try either manual testing or automated as part of unit tests. Manual testing:

Automated testing:

Solution 5 - C#

Is GameController deriving from ApiController ? Are you using WebApi ?

If not then i think the "/api/" is reserved for new WebApi feature. Try changing your routing and controller name to "gameapi"

If however you are using WebApi.

Then remove api from yor BaseUrl

  client = new RestClient
  {
      CookieContainer = new CookieContainer(),
      BaseUrl = "http://localhost:21688/",
  };

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionMech0zView Question on Stackoverflow
Solution 1 - C#Paul EvansView Answer on Stackoverflow
Solution 2 - C#Paul FlemingView Answer on Stackoverflow
Solution 3 - C#Troy DaiView Answer on Stackoverflow
Solution 4 - C#AndreeView Answer on Stackoverflow
Solution 5 - C#Michal FrancView Answer on Stackoverflow