0

ASP.NET MVC 6(DNX 4.5.1).

In my PUT method.

public class MyController : ApiController
{
    .......
// UPDATE
    [Microsoft.AspNet.Mvc.HttpPut]
    public void Update()
    {
        try
        {
            var item = new Item()
            {
                UserName = Request.Form["UserName"]
            };
            _itemRespository.Update(item);
        }
        catch (Exception e)
        {
            Debug.WriteLine(e.Message);
        }
    }

What I got:

Error CS1061 'HttpRequestMessage' does not contain a definition for 'Form' and no extension method 'Form' accepting a first argument of type 'HttpRequestMessage' could be found (are you missing a using directive or an assembly reference?) MyProject.DNX 4.5.1

There is a similar question, but I don't see help me.

EDIT:

The link uses POST to pass the data to the method. He passed string value in the method. In another word, he knows the value in advance. Well, in my case I use jqgrid to edit my data. After I click the submit button, I think that the edited data is from the Request Form. I don't know it yet in advance.

Community
  • 1
  • 1
  • The answer you are linking to is pretty self explanatory. What do you not understand or still have questions on? – Igor Jan 21 '16 at 21:01
  • That is for `POST`, mine is `PUT`. So how can I borrow the code? IF So, please paste your code. –  Jan 21 '16 at 21:02
  • It is the exact same answer whether its `POST`, `PUT`, or `DELETE`. You should pass the data directly using method parameters and an attribute to specify whether its coming from the body or from the uri. The only exception is `GET` where its then only possible to include data in the URL or in the HTTP header as you cannot pass anything in the payload (ie data) of the request. – Igor Jan 21 '16 at 21:05
  • The example passed a string, however in my example I have to pass an object.(POCO). Not sure how? –  Jan 21 '16 at 21:08
  • The exact same way as a string. See this article/how to on working with web apis: https://weblog.west-wind.com/posts/2012/May/08/Passing-multiple-POST-parameters-to-Web-API-Controller-Methods – Igor Jan 21 '16 at 21:09
  • It is different. The article still uses `POST`, he already knows the object to be passed. However in `PUT`, the object is unknown, you can't pass it as an argument in the method. You have to grab it from the `Form`. So in my method, I used `Form`. –  Jan 21 '16 at 21:13
  • The only difference between `PUT` and `POST` is that PUT **should** be used idempotent. Other than that there is no technical difference between it and `POST`. You implement them the same way as far as how you pass data to the web api method and return a response. Again, you should really read a how to or starter on implementing web api so you do not get stuck from the get-go. That is not meant as an insult only a recommendation. – Igor Jan 21 '16 at 21:18
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/101344/discussion-between-love-and-igor). –  Jan 21 '16 at 21:21
  • See my updated question. –  Jan 21 '16 at 21:41
  • It looks like the question is how to retrieve a form value by name if the name is not known at compile time. – Andrew Savinykh Jan 21 '16 at 21:46
  • @zespri, you are right. –  Jan 21 '16 at 21:57

2 Answers2

0

Here are 2 good starting points for how to pass dynamic form content to a web api method.

Sending HTML Form Data in ASP.NET Web API: Form-urlencoded Data

Stackoverflow question - How to get POST data in WebAPI?

So your question should be rephrased as how do you work with dynamic form content and the Web API framework. It has nothing to do with what web method (POST, PUT, DELETE, GET) you are using.

Community
  • 1
  • 1
Igor
  • 60,821
  • 10
  • 100
  • 175
  • Please post your code for my specific case. It is not an answer. It just a comment. –  Jan 21 '16 at 21:58
  • @Love You do not really need an answer, you need a book or some other type of learning/introduction material. At the moment you are looking for quick answers without understanding why something works the way it does. Sometimes this is ok but not when you fail to understand the fundamental concepts of the platform you are developing on/for. Without this core knowledge you will continue to struggle at every turn. I gleaned this based on some of your other questions. I highly recommend you take a couple of hours and read up on Web Api before continuing any further. I wish you the best. – Igor Jan 21 '16 at 22:32
  • The difficult thing is that the html structure is simple when using [jqgrid](http://www.guriddo.net/demo/guriddojs/). `
    `. That's it. We don't know selectors in the form. All the forms structure are hidden in the `jqgrid`. See the [jqgrid demo](http://www.trirand.com/jqgridwiki/doku.php?id=wiki:form_editing).
    –  Jan 22 '16 at 01:08
-1
Microsoft.AspNet.Mvc.HttpPut

ASP.NET MVC 6(DNX) doesn't support to get it. Unless we use

System.Web.Http.HttpPut