1

I am a newbie to VS Code environment. I am developing a Web API in VS Code which would later be consumed from client application. 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

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

Three questions: 1. How I can debug the code? 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? 3. If there is no alternative, please tell me why the method is not hitting.

5
  • which version of webapi you are using? Did you defined a route for multiple parameters? When you look for the endpoint in browser do you get a 404 response code? and also you need to append api in front of you api controllers. Please let me know. I think appending API infront of pwapi should should solve the issue. Commented Dec 28, 2015 at 5:38
  • not sure about webapi version. I am developing in asp.net 5 using dnx. there is no typical 404, but a blank screen is appearing. http://localhost:5000/pwapi/plants/10076 works fine without appending anything but http://localhost:5000/pwapi/plants/10076/features doesn't. "Did you defined a route for multiple parameters" - how can I do this? I am a newbie! :) Commented Dec 28, 2015 at 5:52
  • 1
    why there is a comma in your route prefix? [HttpGet("{id}, {sectionArray}")] Try using [HttpGet("{id}/{sectionArray}")] Commented Dec 28, 2015 at 5:55
  • solution worked perfectly! Thanks a lot! however I would still like to know about debugging c# in VS code. And here is the route [Route("pwapi/[controller]")]. Commented Dec 28, 2015 at 6:02
  • Okay I will write an answer and will write the steps too. Commented Dec 28, 2015 at 6:03

1 Answer 1

2

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.

Sign up to request clarification or add additional context in comments.

7 Comments

Yes, as for debugging I already used the steps you mentioned above. This creates an empty launch.json but it needs additional configurations to set in order to initiate the debugging process. Where I can get the configurations?
can I see the content of launch.json file? If you need to attach to some process that will be "w3wp.exe" for asp.net hosted sites/apis
I updated VS Code. Now launch.json has something in it. However, "program" node is set to "ConsoleApplication.exe" but mine is a Web API.
Instead of consileapplicatikn.exe use w3wp.exe
request 'launch': program 'd:\Projects\aspnet\apiservice\w3wp.exe' does not exist. Do I have to install debugger using locally for this project? If yes how? I have asp.net 5 installed already. Won't VS Code use it from GAC?
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.