简体   繁体   中英

Rest API No action was found on the controller

I call your experience to help me solve a problem. I have this code:

public class FanController : ApiController
{

    [ActionName("ImportAwb")]
    [HttpPost]
    public async Task<object> ImportAwb([FromBody]JObject data) 
    {
        try
        {
            string username = data["username"].ToString();
            string clientId = data["clientId"].ToString();
            string userPass = data["userPass"].ToString();
            string fisier = data["fisier"].ToString();

            var responseString = await FanCourier.ImportAwbIntegrat(username, clientId, userPass, fisier);
            return Ok(responseString);
        }
        catch (Exception ex)
        {
            return (ex);
        }
    }

If i left just one method like this, i can call it from Postman with no problem, but if i try to make another one, like this:

{

public class FanController : ApiController
{

    [ActionName("ImportAwb")]
    [HttpPost]
    public async Task<object> ImportAwb([FromBody]JObject data) 
    {
        try
        {
            string username = data["username"].ToString();
            string clientId = data["clientId"].ToString();
            string userPass = data["userPass"].ToString();
            string fisier = data["fisier"].ToString();

            var responseString = await FanCourier.ImportAwbIntegrat(username, clientId, userPass, fisier);
            return Ok(responseString);
        }
        catch (Exception ex)
        {
            return (ex);
        }
    }


    [ActionName("PrintareAwbHtml")]
    [HttpPost]
    public async Task<object> PrintareAwbHtml([FromBody]FanCourier fanCourier)
    {
        try
        {
            var responseString =
                await fanCourier.PrintareAwbHtml(fanCourier);
            return Ok(responseString);
        }
        catch (Exception ex)
        {
            return (ex);
        }

    }

The response from Postman call is:

"Multiple actions were found that match the request: \r\nImportAwb on type Courier.Rest.Controllers.FanController\r\nPrintareAwbHtml on type Courier.Rest.Controllers.FanController"

I was tried to add a [Route("api/[controller]")] before public class FanController : ApiController and the error was change to:

No action was found on the controller 'Fan' that matches the request.

I have tried to find something on the internet but i found nothing to help my situations.

You should define routes for each of your actions so you know which will be called when specific API is called.

Use [Route] tag to accomplish that

    [ActionName("ImportAwb")]
    [HttpPost]
    [Route("Action1")]
    public async Task<object> ImportAwb([FromBody]JObject data) 
    {
...

And from postman, call your endpoint with url being. http://yoururl.com/Action1 .

(YourUrl would be the path you set up for this controller... might include /api or what you might have configured. Add /Action1 to the end of that url)

You can have multiple routes to the same URL as long as they are different methods (post, get, delete, patch etc.).

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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