简体   繁体   中英

Passing XML String to Web API Results in 404 Not Found

I'm trying to send a string to my Web API. If I use a string literal for fileContents (adsf), it works:

        var config = {
            url: '/PrestoWeb/api/app/importTasks' + '?fileContents=adsf',
            method: 'POST'
        };

        $http(config)
            .then(function (response) {
                // success
            }, function (response) {
                // fail
            });

fileContents shows up correctly in the Web API:

    [AcceptVerbs("POST")]
    [Route("api/app/importTasks")]
    public Application ImportTasks(string fileContents)
    {
        // fileContents is adsf

If I change the url to my variable, I get 404 Not Found:

url: '/PrestoWeb/api/app/importTasks' + '?fileContents=' + fileContents,

I also tried this:

url: '/PrestoWeb/api/app/importTasks' + '?fileContents=' + encodeURIComponent(fileContents),

And this:

url: '/PrestoWeb/api/app/importTasks' + '?fileContents=' + encodeURIComponent(JSON.stringify(fileContents)),

Still get a 404 Not Found. What am I missing?

Note: The actual fileContents variable contains a string in XML format. Here is the first part of it:

"<ArrayOfTaskBase z:Id=\"1\" z:Type=\"System.Collections.Generic.List`1[[PrestoCommon.Entities.TaskBase, PrestoCommon, Version=3.6.0.0, Culture=neutral, PublicKeyToken=null]]\" z:Assembly=\"0\"

Your fileContents string contains dots, rendering ASP.NET router unable to match the URL .

See that thread for the solution :

<add name="ApiURIs-ISAPI-Integrated-4.0"
     path="/PrestoWeb/api/app/*"
     verb="POST"
     type="System.Web.Handlers.TransferRequestHandler"
     preCondition="integratedMode,runtimeVersionv4.0" />

(or, as Igor wrote in comments, just send the string in the request body)

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