How do you debug MVC 4 API routes?

前端 未结 5 1854
孤独总比滥情好
孤独总比滥情好 2020-12-07 23:57

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 fa

相关标签:
5条回答
  • 2020-12-08 00:29

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

    http://nuget.org/packages/routedebugger

    0 讨论(0)
  • 2020-12-08 00:30

    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.

    0 讨论(0)
  • 2020-12-08 00:36

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

    • RouteDebugger
    • MVC API tracing

    Automated testing:

    • MvcRouteTester.
    0 讨论(0)
  • 2020-12-08 00:38

    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/",
      };
    
    0 讨论(0)
  • 2020-12-08 00:41

    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/

    0 讨论(0)
提交回复
热议问题