kianoush
kianoush

Reputation: 99

404 Not Found when accessing a Web API method

I need this method to return an integer value:

[Route("api/[controller]")]
public class ValuesController : Controller
{
    [HttpPost("ByPayment")]
    public int Payment(string accountId, string mount, string shenase)
    {
        return 21;
    }
}

When I go to the following address:

http://localhost:1070/api/values/Payment/ByPayment?accountId=258965&mount=85694&shenase=85456

I get the following error:

ٍError

What's the problem? And how can I solve it?

Upvotes: 2

Views: 7760

Answers (2)

Ahmar
Ahmar

Reputation: 3867

I thing you wanted to send Get request with query string parameters.

1. Change the 'HttpPost' to 'HttpGet' [HttpPost("ByPayment")] to [HttpGet("ByPayment")]

2. Also change your request url, Its not correct.

http://localhost:1070/api/values/Payment/ByPayment?accountId=258965&mount=85694&shenase=85456

to

http://localhost:1070/api/Values/ByPayment?accountId=258965&mount=85694&shenase=85456

Updated code

[Route("api/[controller]")]
public class ValuesController : Controller
{
    [HttpGet("ByPayment")]
    public int Payment(string accountId, string mount, string shenase)
    {
        return 21;
    }
}

I suggest please read this tutorial to understand the basic of webapi.

Upvotes: 3

rocky
rocky

Reputation: 7696

There could be more reasons why you get the 404. But there is one thing that's definitely wrong - you are sending GET requests to a method that's marked with [HttpPost("ByPayment")] (which means it only responds to POST requests.

I don't know what you intended to do but you either have to change it to [HttpGet("ByPayment")] or use a REST client that can make POST requests (e.g. REST Easy.

Other reason could be that your controller has a wrong name. It should be called PaymentController.

Upvotes: 2

Related Questions