Simple controller which takes POST is not found

前端 未结 1 1044
轻奢々
轻奢々 2021-02-03 17:53

I\'ve made some previous question asking for the help with the problems since I updated MVC4 webapi beta to RC. I got most in order now, but here\'s one I cannot figure out the

相关标签:
1条回答
  • 2021-02-03 18:15

    Web.API is a little bit picky when you want to post "simple" values.

    You need to use the [FromBody] attribute to signal that the value is not coming from the URL but from the posted data:

    [HttpPost]
    public string Test([FromBody] string output)
    {
        return output;
    }
    

    With this change you won't get 404 anymore but output will be always null, because Web.Api requries the posted values in special format (look for the Sending Simple Types section):

    Second, the client needs to send the value with the following format:

    =value

    Specifically, the name portion of the name/value pair must be empty for a simple type. Not >all browsers support this for HTML forms, but you create this format in script...

    So recommend that you should create a model type:

    public class MyModel
    {
        public string Output { get; set; }
    }
    
    [HttpPost]
    public string Test(MyModel model)
    {
        return model.Output;
    }
    

    Then it will work with your sample froms without modifing your views.

    0 讨论(0)
提交回复
热议问题