2
votes

I'm trying to create my first REST application with the web api in mvc4. I have a controller set up with the HttpPost verb but for some reason when I click the link to post the xml string to the controller I get an error - "{"Message":"No HTTP resource was found that matches the request URI '/api/Apply/ApplyToJob'.","MessageDetail":"No action was found on the controller 'Apply' that matches the request."}" Any ideas what I might be doing wrong? Here's the view page...

<a href="javascript:void(0);" id="lnkPost">Post Data</a>

<script type="text/javascript">
    window.onload = function () {
        $("#lnkPost").on("click", function () {

            $.get("/TestResponse.xml", function (d) {
                $.ajax({
                    //contentType: "text/xml",
                    //dataType: "xml",
                    type: "post",
                    url: "/api/Apply/ApplyToJob",
                    data: {
                        "strXml": (new XMLSerializer()).serializeToString(d)
                    },
                    success: function () { console.log('success'); }
                });
            });
        });
    };
</script>

and here's the controller.

public class ApplyController : ApiController
{
    [HttpPost]
    [ActionName("ApplyToJob")]
    public string ApplyToJob(string strXml)
    {
        return "success";
    }
}
1
Just post to /api/Apply - Maess
Also, you may want to do some additional reading, as the approach you are taking isn't really RESTFUL. - Maess
I assume you mean because the method isn't named Post? - geoff swartz

1 Answers

2
votes

Modify your action's parameter like below:

public string ApplyToJob([FromBody]string strXml)

This is because without this FromBody attribute, the string parameter is expected to come from the Uri. Since you do not have it in the Uri the action selection is failing.

Also, looking at your client code, shouldn't you set the appropriate content type header in your request?

Edited:

You can modify your javascript to like below and see if it works:

$.ajax({    contentType: "text/xml",
            dataType: "xml",
            type: "post",
            url: "/api/values",
            data: "your raw xml data here",
            success: function () { console.log('success'); }
        });