如何最好地调试MVC 4 API路由?

时间:2022-11-12 23:22:07

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.

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

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.

这是一个例子,我的GameController的构造函数被击中了,但是Post方法没有被击中,我不明白为什么。

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

5 个解决方案

#1


31  

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

RouteDebugger能够很好地确定哪些路由不会被击中。

http://nuget.org/packages/routedebugger

http://nuget.org/packages/routedebugger

#2


76  

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:

另一种方法是在Global.asax中添加一个事件处理程序。cs来接收传入的请求,然后查看VS调试器中的路由值。重写Init方法如下:

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.

然后在showRouteValues中设置一个断点,并查看routeData的内容。

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

请记住,在Web API项目中,Http路由位于WebApiConfig中。cs,不是RouteConfig.cs。

#3


2  

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/

你可以试试ASP。NET Web API路由调试器。它是为Web Api编写的。https://trocolate.wordpress.com/2013/02/06/introducing-asp-net-web-api-route-debugger/ http://nuget.org/packages/WebApiRouteDebugger/

#4


2  

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:

自动化测试:

#5


0  

Is GameController deriving from ApiController ? Are you using WebApi ?

GameController是从ApiController派生出来的吗?你在使用WebApi吗?

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

如果没有,我认为“/api/”是为新的WebApi特性预留的。尝试将路由和控制器名称更改为“gameapi”

If however you are using WebApi.

如果您正在使用WebApi。

Then remove api from yor BaseUrl

然后从BaseUrl中删除api

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

#1


31  

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

RouteDebugger能够很好地确定哪些路由不会被击中。

http://nuget.org/packages/routedebugger

http://nuget.org/packages/routedebugger

#2


76  

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:

另一种方法是在Global.asax中添加一个事件处理程序。cs来接收传入的请求,然后查看VS调试器中的路由值。重写Init方法如下:

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.

然后在showRouteValues中设置一个断点,并查看routeData的内容。

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

请记住,在Web API项目中,Http路由位于WebApiConfig中。cs,不是RouteConfig.cs。

#3


2  

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/

你可以试试ASP。NET Web API路由调试器。它是为Web Api编写的。https://trocolate.wordpress.com/2013/02/06/introducing-asp-net-web-api-route-debugger/ http://nuget.org/packages/WebApiRouteDebugger/

#4


2  

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:

自动化测试:

#5


0  

Is GameController deriving from ApiController ? Are you using WebApi ?

GameController是从ApiController派生出来的吗?你在使用WebApi吗?

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

如果没有,我认为“/api/”是为新的WebApi特性预留的。尝试将路由和控制器名称更改为“gameapi”

If however you are using WebApi.

如果您正在使用WebApi。

Then remove api from yor BaseUrl

然后从BaseUrl中删除api

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