简体   繁体   English

如何使用Visual Studio Code调试C#代码

[英]How to debug C# code using Visual Studio Code

I am a newbie to VS Code environment. 我是VS Code环境的新手。 I am developing a Web API in VS Code which would later be consumed from client application. 我正在用VS Code开发Web API,以后将从客户端应用程序中使用它。 Following code block does not seem to be executing so I want to check whether it is at all hitting! 以下代码块似乎没有执行,因此我想检查一下是否全部成功!

 //GET: pwapi/plants/10076/features [HttpGet("{id}, {sectionArray}")] public async Task<string> Get(int id, string sectionArray){ var url="https://www.domain.com/search?apikey=<apikey>&sections="+sectionArray+"&plantid="+id; using(var client = new HttpClient()){ client.BaseAddress=new Uri(url); client.DefaultRequestHeaders.Accept.Clear(); var response=await client.GetAsync(url); //will throw an exception if not successful response.EnsureSuccessStatusCode(); string content = await response.Content.ReadAsStringAsync(); //return await Task.Run(() => JsonObject.Parse(content)); return content; } } 

URL for the above method is http://localhost:5000/pwapi/plants/10076/features 上述方法的URL是http:// localhost:5000 / pwapi / plants / 10076 / features

I have the overloaded method which is working fine: 我有工作正常的重载方法:

 [HttpGet("{id}")] public async Task<string> Get(int id){ var url="https://www.domain.com/list?apikey=<apikey>"; using(var client = new HttpClient()){ client.BaseAddress=new Uri(url); client.DefaultRequestHeaders.Accept.Clear(); var response=await client.GetAsync(url); //will throw an exception if not successful response.EnsureSuccessStatusCode(); string content = await response.Content.ReadAsStringAsync(); //return await Task.Run(() => JsonObject.Parse(content)); return content; } } 

URL for the above method is http://localhost:5000/pwapi/plants/10076 上述方法的URL是http://localhost:5000/pwapi/plants/10076

Three questions: 1. How I can debug the code? 三个问题:1.如何调试代码? 2. If debug is not possible (so far I have seen that debug is possible for client scripts only, I may be wrong), what is the alternative way to know where I am doing wrong? 2.如果无法进行调试(到目前为止,我已经看到调试仅适用于客户端脚本,我可能是错的),那么知道我在哪里做错的另一种方法是什么? 3. If there is no alternative, please tell me why the method is not hitting. 3.如果没有其他选择,请告诉我为什么该方法没有奏效。

If you notice your route prefix for Get overload you have a comma instead of a slash, which makes the route invalid therefore your endpoint never gets hit. 如果您注意到“获取超载”的路由前缀,则使用逗号而不是斜杠,这会使路由无效,因此您的端点永远不会被命中。 Please change the route prefix to this: 请将路由前缀更改为此:

[HttpGet("{id}/{sectionArray}")]

As for debugging the endpoint you just need to put a break point by either pressing F9 on the line or by clicking on the left of the line of code that you want to debug. 至于调试端点,您只需要通过在该行上按F9或单击要调试的代码行的左侧来放置一个断点即可。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM