0

Below is my controller and action:

[RoutePrefix("api/PaymentManagementController")]
public class PaymentManagementController : ApiController
{
    [HttpGet]
    [Route("")]
    public HttpResponseMessage CheckStatus(string commandType, string account, string txnId)
    {
    }
}

Here is WebApi.config:

config.Routes.MapHttpRoute(
    name: "DefaultApi",
    routeTemplate: "api/{controller}/{action}/",
    defaults: new { id = RouteParameter.Optional }

This link is working:

localhost:59253/api/PaymentManagementController?commandType=check&account=ParamValue1&txnId=SomePrefix0123456789

However, I expected it as:

localhost:59253/api/PaymentManagementController/CheckStatus?commandType=check&account=ParamValue1&txnId=SomePrefix0123456789

Even after marking Routing as below:

[RoutePrefix("api/PaymentManagementController")]
public class PaymentManagementController : ApiController
{
    [HttpGet]
    [Route("CheckStatus/{commandType}/{account}/{txnId}")]
    public HttpResponseMessage CheckStatus(string commandType, string account, string txnId)
    {
    }
}

I receive error as:

No HTTP resource was found that matches the request URI 'localhost:59253/api/PaymentManagementController?commandType=check&account=ParamValue1&txnId=SomePrefix0123456789'. No action was found on the controller 'PaymentManagement' that matches the request.

1
  • I believe the route you should test for "CheckStatus/{commandType}/{account}/{txnId}" should be 'localhost:59253/api/PaymentManagementController/CheckStatus/check/ParamValue1/SomePrefix0123456789' given your second routing, the first one, the controller is working correctly because you assigned null string the route to the method. Commented Jan 8, 2019 at 16:10

5 Answers 5

1

Change your route to

[HttpGet]
    [Route("CheckStatus")]
    public HttpResponseMessage CheckStatus(string commandType, string account, string txnId)
    {
    }
Sign up to request clarification or add additional context in comments.

Comments

1

as I posted above,

[RoutePrefix("api/PaymentManagementController")]
public class PaymentManagementController : ApiController
{
    [HttpGet]
    [Route("CheckStatus")]
    public HttpResponseMessage CheckStatus(string commandType, string account, string txnId)
    {
    }

use the test url:

localhost:59253/api/PaymentManagementController/CheckStatus?commandType=check&account=ParamValue1&txnId=SomePrefix0123456789

but the second pattern you tried is more modern:

[RoutePrefix("api/PaymentManagementController")]
public class PaymentManagementController : ApiController
{
    [HttpGet]
    [Route("CheckStatus/{commandType}/{account}/{txnId}")]
    public HttpResponseMessage CheckStatus(string commandType, string account, string txnId)
    {
    }
}

you should test this using the URL

localhost:59253/api/PaymentManagementController/CheckStatus/check/ParamValue1/SomePrefix0123456789

Comments

1

You define attributes on your controller and it works as expected using Attribute Routing. The defined url in attribute routing works like this : Route Prefix / Route / Parameters and for your code it will work for api/PaymentManagementController?commandType=check&account=ParamValue1&txnId=SomePrefix0123456789.

To achieve the requested url for work, try this code:

[RoutePrefix("api/PaymentManagementController")]
public class PaymentManagementController : ApiController
{
    [HttpGet]
    [Route("CheckStatus")]
    public HttpResponseMessage CheckStatus(string commandType, string account, string txnId)
    {
    }
}

Comments

1

Remove "Controller" from RoutePrefix or remove the whole attribute - controllers are mapped automatically.

[RoutePrefix("api/PaymentManagement")]

To go with the expected link without controller:

localhost:59253/api/PaymentManagement/CheckStatus?commandType=check&account=ParamValue1&txnId=SomePrefix0123456789

I would go with:

public class PaymentManagementController : ApiController
{
    public HttpResponseMessage CheckStatus(string commandType, string account, string txnId)
    {
    }
}

To go with the expected link with controller:

localhost:59253/api/PaymentManagementController/CheckStatus?commandType=check&account=ParamValue1&txnId=SomePrefix0123456789

public class PaymentManagementControllerController : ApiController
{
    public HttpResponseMessage CheckStatus(string commandType, string account, string txnId)
    {
    }
}

Comments

0
    Try it.
    [HttpGet]
    [Route("CheckStatus")]
    public HttpResponseMessage CheckStatus(string commandType, string account, string txnId)
    {
    }

1 Comment

please try to add the explanation that what you have done in the answer.

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.