85

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
            }             
        );
2

5 Answers 5

121

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.

Sign up to request clarification or add additional context in comments.

4 Comments

Quick and dirty, just what I needed!
This doesn't scale to a solution with dozens/hundreds of controllers.
Where is this in aspnet6?
In .Net 8, we inserted a piece of middleware into the application pipeline before app.MapControllerRoute. In the middleware, you have access to HttpContext and can do something similar. There are online examples that show how to insert your own middleware.
37

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

http://nuget.org/packages/routedebugger

5 Comments

Already proposed in comments above, but I cant see how that should tell me where it goes wrong? 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?
According to your screenshot, gameId is not optional. Try including gameId and see if it hits your route...
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).
Rename your post parameter to gameId.
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))
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/

4 Comments

Note this package is for c#. Does not seem to support vb.net.
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.
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.
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.
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:

Comments

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/",
  };

5 Comments

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 pastebin.com/qki60LyU
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 - nuget.org/packages/elmah
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)
is your Post action decorated by [HttpPost] attribute ?
No pastebin.com/qki60LyU havent been necessary before, and my Get requests work and I just tried using it but no dice

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.