81

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
            }             
        );
Liam
  • 27,717
  • 28
  • 128
  • 190
Mech0z
  • 3,627
  • 6
  • 49
  • 85

5 Answers5

117

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.

Maria Ines Parnisari
  • 16,584
  • 9
  • 85
  • 130
Paul Evans
  • 1,436
  • 2
  • 10
  • 13
36

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

http://nuget.org/packages/routedebugger

Paul Fleming
  • 24,238
  • 8
  • 76
  • 113
  • Already proposed in comments above, but I cant see how that should tell me where it goes wrong? http://www.gratisimage.dk/graphic/images/2012/July/13/C044_50005206.jpg this is what I get with that and loookign at api/game/{gameId} controller = game, gameId = the stuff I did should work? – Mech0z Jul 13 '12 at 16:52
  • According to your screenshot, gameId is not optional. Try including gameId and see if it hits your route... – Paul Fleming Jul 13 '12 at 16:56
  • Note also that your Post action cannot accept an optional gameId. Consider either making it mandatory or making the parameter nullable (int? gameId) or setting a default value (int gameId = 0). – Paul Fleming Jul 13 '12 at 16:58
  • 1
    Rename your post parameter to gameId. – Paul Fleming Jul 13 '12 at 17:16
  • That dont do anything (I only renamed it because I tried removing the parameter (I dont use it, was only added to try to get it to hit the method)) – Mech0z Jul 13 '12 at 18:35
1

You can try ASP.NET Web API Route Debugger. It is written for Web Api. https://trocolate.wordpress.com/2013/02/06/introducing-asp-net-web-api-route-debugger/ http://nuget.org/packages/WebApiRouteDebugger/

Troy Dai
  • 2,071
  • 1
  • 13
  • 7
  • Note this package is for c#. Does not seem to support vb.net. – Tim Murphy Mar 21 '13 at 04:20
  • 6
    Tried this. Did not work for my ASP.NET MVC 5 project with Web API added on. Then I completely messed up my project when I uninstalled it due to its many dependencies. Consider it install-only. – Rap Aug 13 '14 at 21:13
  • 1
    It messed-up even my Web API 2 project - why does a debugging tool need to add Razor, Twitter Bootstrap and other add-ons to a strictly JSON webservice? Phil Haack's tool is better, and very minimal. – Dai Sep 01 '16 at 01:54
  • 1
    I accidentally installed the package for the wrong project (just a dll library project), which couldn't have had routes debugged anyways. So I had twice as much leftover junk to clean up after I found it didn't work with MVC 5. – stannius Sep 18 '17 at 17:26
1

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:

Andree
  • 1,159
  • 2
  • 17
  • 32
0

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/",
  };
Michal Franc
  • 1,036
  • 10
  • 16
  • Yes I have it working, but its just when I have some that dont work I cant figure out a good way to do it but my controller is like this http://pastebin.com/qki60LyU – Mech0z Jul 13 '12 at 16:44
  • Then you can use RouteDebugger from "flem s" link it's the same thing from my link :) Just run it on your page that is used by WP7, do it in the browser. Also you can think about installing Elmah - http://nuget.org/packages/elmah – Michal Franc Jul 13 '12 at 16:49
  • That just gives me an XML file (Therefor now information from routedebugger) and also when I run it I hit my Get and not my Post (Which works fine) – Mech0z Jul 13 '12 at 17:00
  • is your Post action decorated by [HttpPost] attribute ? – Michal Franc Jul 13 '12 at 17:02
  • No http://pastebin.com/qki60LyU havent been necessary before, and my Get requests work and I just tried using it but no dice – Mech0z Jul 13 '12 at 17:09