您如何调试MVC 4 API路由? [英] How do you debug MVC 4 API routes?

查看:87
本文介绍了您如何调试MVC 4 API路由?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个使用RESTsharp与我的MVC4 RESTful服务器通信的WP7游戏,但是我经常遇到使请求正常工作的问题,因此我想调试失败的地方。

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.

这是一个示例,其中我的 GameController 上的构造方法被命中,但 Post 方法未命中,

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.

客户代码:

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) =>
  {});
}

服务器代码:

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

我的路线是这样

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


推荐答案

RouteDebugger可以很好地弄清楚哪些路由将/将

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

http://nuget.org/包/ routedebugger

这篇关于您如何调试MVC 4 API路由?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆