1

i have a route:

routes.MapRoute (name: "apicontroller2",
                 url: "api/{controller}/{action}/",
                 defaults: new { controller = "Default2", action = "Index" }
                );

and the Default2Controller with two post methods:

[HttpPost] 
public HttpResponseMessage Post(ttReview review)
{
...
}

[HttpPost] 
public HttpResponseMessage PostPro(ttbewertungenpro pro)
{
...
}

when i call these API-Methods via webserver/api/default2/PostPro/ or webserver/api/default2/Post/ ajax post i get the error:

ExceptionMessage=Multiple actions were found that match the request: 
System.Net.Http.HttpResponseMessage Post(WT.Models.ttReview) on type WT.Controllers.Default2Controller
System.Net.Http.HttpResponseMessage PostPro(WT.Models.ttbewertungenpro) on type WT.Controllers.Default2Controller

My ajax calls are:

$.ajax({
        url: "../api/default2/Post", // or "../api/default2/PostPro",
        type: 'POST',
        dataType: "text", 
        data: review, // or pro
        success: function (test) {
        },
        error: function (test) {
            alert("Error");
        }
}

is my route wrong or what? when i delete one method the other works...

3
  • 1
    Posts for /Default2/ will look for Default2Controller, but your controller is named DefaultController2. Commented Jan 17, 2013 at 10:18
  • What happens when you rename PostPro to Test to get rid of the common prefix Post? Commented Jan 17, 2013 at 10:26
  • Are you sure you need the .. in-front of API in the URL ? Commented Jan 17, 2013 at 12:25

2 Answers 2

2

Check out AttributeRouting (http://nuget.org/packages/AttributeRouting.WebApi)

It is so much easier to map your routes.

Example

[POST("Page/Method")]
public HttpResponseMessage Post(ttReview review)
{
...
}
Sign up to request clarification or add additional context in comments.

1 Comment

this solves the problem, but i still have to define each route manually. This is more a workaround than the solving of the initial problem. So i don't want to mark it as an answer yet.
1

With a bran new blank WebAPI Project - I have not touched the global.asax file.

Home Controller

[HttpPost]
public HttpResponseMessage Post(string review)
{
    return new HttpResponseMessage(HttpStatusCode.Created);
}

[HttpPost]
public HttpResponseMessage PostPro(string pro)
{
    return new HttpResponseMessage(HttpStatusCode.Created);
}

Index.cshtml

<script src="~/Scripts/jquery-1.7.1.js"></script>

<script>

    $(document).ready(function () {

        $("#submit").click(function() {

            $.ajax({
                url: "home/Post", // or "../api/default2/PostPro",
                type: 'POST',
                dataType: "text",
                data: "test", // or pro
                success: function(test) {
                },
                error: function(test) {
                    alert("Error");
                }
            });

        });

    });


 </script>


<input type="submit" value="submit" id="submit" />

This works fine for me, so the problem must be your path/routes.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.