2

I currently have an issue with a webapi call. I want to download and open a logfile with my ApiController. I use a javascript function to post a filename to my controller.

Here is a helper function to post the parameter (answer from dystroy): How to replace window.open(...) with a POST

Now when I use a simple string as parameter in my controller I can’t get the parameter, it is always null.

public HttpResponseMessage PostDownloadLogFile([FromBody]string psFileName) { //psFileName is always null 

But if I use HttpReqeustMessage as parameter and read the form data from my request it is no problem and it works.

public HttpResponseMessage PostDownloadLogFile(HttpRequestMessage poRequest) { var loFormData = poRequest.Content.ReadAsFormDataAsync().Result; string psFileName = loFormData["psFileName"]; //psFileName is set correct 

Is there a solution to get the parameter with a simple parameter in my controller?

Update

This is my javascript helper function:

var loOpenWindow = function (psMethode, psUrl, poData, psTarget) { var loForm = document.createElement("form"); loForm.action = psUrl; loForm.method = psMethode; loForm.target = psTarget || "_self"; if (poData) { for (var lsKey in poData) { var loInput = document.createElement("textarea"); loInput.name = lsKey; loInput.value = typeof poData[lsKey] === "object" ? JSON.stringify(poData[lsKey]) : poData[lsKey]; loForm.appendChild(loInput); } } loForm.style.display = "none"; document.body.appendChild(loForm); loForm.submit(); }; 

Call it:

helper.openWindow("POST", apiRoutes.URLS.ApiPostDownloadLogFile, { "psFilename": $scope.data.showLogEntry.FullName }); 

There should be no problem from the client side code, because the controller methode with HttpReqeustMessage works without problems.

Here is the browser request: enter image description here

6
  • What does the data you're sending to the controller method look like? Commented Feb 20, 2015 at 16:02
  • That is a javascript help function from this post: stackoverflow.com/questions/17793183/… Commented Feb 20, 2015 at 16:05
  • The helper function has four parameters: verb, url, data, and target. What are you passing into the data variable (and all the others for that matter)? Commented Feb 20, 2015 at 16:07
  • helper.openWindow("POST", apiRoutes.URLS.ApiPostDownloadLogFile, { "psFilename": $scope.data.showLogEntry.FullName }); Commented Feb 20, 2015 at 16:10
  • 1
    Can you please paste here the browser request? With HTTP headers and body. Commented Feb 20, 2015 at 16:16

2 Answers 2

2

Probably the problem is in your client code sending the data.

[FromBody] parameters must be encoded as =value

then, this does not work:

// Value will be null. $.post('api/values', value); // Value will be null. $.post('api/values', { key: value }); 

But this work:

$.post('api/values', "=" + value); 

Try to change your client code to send just =/path/to/your/file in the body.

Reference: http://encosia.com/using-jquery-to-post-frombody-parameters-to-web-api/

Sign up to request clarification or add additional context in comments.

1 Comment

I try it but unfortunately the value is still null. I try this: helper.openWindow("POST", apiRoutes.URLS.ApiPostDownloadLogFile, "=" + $scope.data.showLogEntry.FullName); and this: helper.openWindow("POST", apiRoutes.URLS.ApiPostDownloadLogFile, { '': $scope.data.showLogEntry.FullName });
1

Ok I found a solution. If I use a class as parameter and a property with the given name, it seems to work.

public class Param { public string psFileName { get; set; } } 

And

public HttpResponseMessage PostDownloadLogFile(Param poParam) { string psFileName = poParam.psFileName; //psFileName is set correct 

This is not really a simple parameter but I can live with this solution.

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.