C# 你如何调试 MVC 4 API 路由?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/11473038/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How do you debug MVC 4 API routes?
提问by Mech0z
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 GameControlleris hit, but the Postmethod is not hit, and I don't understand why.
这是一个例子,我的ConstructorGameController被命中了,但是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
}
);
采纳答案by Paul Fleming
RouteDebugger is good for figuring out which routes will/will not be hit.
RouteDebugger 非常适合确定哪些路由会/不会被命中。
回答by Michal Franc
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/",
};
回答by Troy Dai
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/
回答by Paul Evans
Another way is to add an event handler in Global.asax.csto pick up the incoming request and then look at the route values in the VS debugger. Override the Initmethod 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 showRouteValuesand 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。
回答by Andree
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:
自动化测试:

