3

I have simple console application, which send json data to controller.

Client.PostAsJsonAsync("http://[site].azurewebsites.net/api/Decision", report);

And controller:

public class DecisionController : ApiController
{
    private readonly TaskManager _taskManager = new TaskManager();
    [HttpGet]
    public Decision Get(int id)
    {
        return _taskManager.GetDecision(id);
    }
    [HttpPost]
    public void Post(Decision decision)
    {
        _taskManager.UpdateDecision(decision);
    }
}

Visual Studio debugger shows, that a request does not reach the controller (not trigger a breakpoint). Console application does not throw any exceptions, and report variable does not equal to null.

How can I debug this?

1 Answer 1

0

You might want to take a look at Fiddler which is a free tool to debug web requests. Basically, it monitors all web requests going through your machine and lets you see what they look like. You can also generate new ones straight from there too. That way, you can at least make sure that your request is properly formatted.

Also, I noticed that you are trying to call your controller directly when you should be calling an action on your controller instead. When you request GET api/Decision, you also need to provide an action method. I do not know enough of your model to restructure your controller properly, but here is an example based on a simple user profile.

public class ProfileController : ApiController
{
    [HttpGet]
    public string FullName()
    {
        return "Foo Bar";
    }

    [HttpPost]
    public void FullName(string newName)
    {
        // ...
    }

    [HttpGet]
    public int Age()
    {
        return 22;
    }

    [HttpPost]
    public void Age(int newAge)
    {
        // ...
    }
}

As you can see, the HttpVERB attribute is not used to dispatch requests to a controller to specific methods. It is used to distinguish the different overloads of related restful actions. In this case, it is used to distinguish between getting and setting a FullName and an Age property.

If you with to keep your controller intact, you should be requesting:

  • POST http://[site].azurewebsites.net/api/Decision/post
  • GET http://[site].azurewebsites.net/api/Decision/get
Sign up to request clarification or add additional context in comments.

6 Comments

Fiddler shows, that json correctly compiled and included in the request.
I see that you have two methods in there but your request does not specify any method. It should be in the form of domain/site/controller/method. To call your code as it is now, you need to do send your requests to http://[site].azurewebsites.net/api/Decision/get or http://[site].azurewebsites.net/api/Decision/post. Obviously, this is not restful so you might want to improve your controller structure.
As far as I know, asp.net can choose the appropriate method depending on the type of request (post, for exemple). I've marked the method attribute [HttpPost].
It can distinguish between overloads, but it cannot know which methods to call inside your controller. You can have multiple methods marked with HttpGet in the same controller. For instance, you could have an Auth controller with a SignIn and SignUp methods both in HttpPost.
Just a quick question, how are you debugging your application? If you are debugging locally, you will have to update your endpoints to point to the local instance of iisexpress running on your machine. If you want to debug remotely on the azure servers, you will need to follow these instructions: blogs.msdn.com/b/webdev/archive/2013/11/05/…
|

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.