0

I've read these questions:

Multiple actions were found that match the request: webapi
Web API Routing - multiple actions were found that match the request Multiple actions were found that match the request Web API?

And I've updated my RouteConfig as follows:

routes.MapRoute(
    name: "Receipt",
    url: "api/{controller}/{action}/{id}",
    defaults: new { controller = "Receipt", action = "Post", source = UrlParameter.Optional }
);

Creating this new route map hasn't helped to fix the problem in the slightest

Here's the relevant functions in the Controller:

public HttpResponseMessage Post ([FromBody]CreateReceiptViewModel source)
{
    try
    {
        // stuff happens here
    }
    catch (Exception ex)
    {
        // something went wrong, save exception data to the database
        // RecordError(ex, "Post Receipt", "ReceiptController/Post");
    }
}

//public void RecordError(Exception ex, string action = "", string origin = "PocketPlooto API")
//{
//    Error Error = new Error
//    {
//        Date = DateTime.Now,
//        Detail = ex.ToString(),
//        Message = ex.Message,
//        Origin = origin,
//        StackTrace = ex.StackTrace,
//        Action = action
//    };

//    db.Errors.Add(Error);
//    db.SaveChanges();
//}

The function is fairly large so I feel it would be impractical to include here.

Interestingly enough, while RecordError is commented, the post happens fine.

If I uncomment it, the error occurs (my test environment doesn't allow me to see it in a format other than JSON, apologies for the inconvenience):

"Message":"An error has occurred.",
"ExceptionMessage":"Multiple actions were found that match the request: \r\nPost on type WebApiService.Controllers.ReceiptController\r\nRecordError on type WebApiService.Controllers.ReceiptController",
"ExceptionType":"System.InvalidOperationException",
"StackTrace":" at System.Web.Http.Controllers.ApiControllerActionSelector.ActionSelectorCacheItem.SelectAction(HttpControllerContext controllerContext)\r\n at System.Web.Http.Controllers.ApiControllerActionSelector.SelectAction(HttpControllerContext controllerContext)\r\n at System.Web.Http.ApiController.ExecuteAsync(HttpControllerContext controllerContext, CancellationToken cancellationToken)\r\n at System.Web.Http.Dispatcher.HttpControllerDispatcher.d__1.MoveNext()"

Unfortunately I don't really understand the stack trace and since there clearly isn't any duplicate actions here, I can't even begin to guess what Web Api is doing or why having this problem in the controller is a problem.

Can anyone provide any clarity on what the stack trace means and why the extra function is a problem?

Community
  • 1
  • 1
Ortund
  • 8,095
  • 18
  • 71
  • 139
  • Why not using routing attributes? I suspect it maps both your method Post and RecordError to the same URL – Jonny Jul 28 '16 at 11:46
  • That's really weird because RecordError should be an internal method only. Clients should never know of its existence. Could be because I mistakenly made RecordError a public method? – Ortund Jul 28 '16 at 11:48
  • Try to make it private then – Jonny Jul 28 '16 at 11:51
  • Do you have only these 2 methods in your controller? – Thangadurai Jul 28 '16 at 11:54
  • No, there's some others. All private. Apparently making the functions private disqualify them as candidates for routing so making the `RecordError` method private seems to have fixed the problem – Ortund Jul 28 '16 at 12:27

1 Answers1

2

If the private methods are need not to be accessed from outside then set [NonAction] attribute on that methods.

ravindra
  • 322
  • 3
  • 14