views:

296

answers:

4

Hi folks,

i wish to have a simple Action in my controller that accepts a few optional values and some integer values.

this is my route i wish to have:

HTTP.POST
/review/create

and this is the Action method i would like...

[AcceptVerbs(HttpVerbs.Post)]
public JsonResult Create(int userId,
                         int addressId,
                         byte baseScore,
                         byte reviewType,
                         string subject,
                         string description)
{ ... }

I'm under the uneducated impression that all of those arguments above will be populated by the forms collection values ... but it's not happening. Also, I have no idea how I would write a route, to handle those ... because those values are form post data....

here's my global.asax....

routes.IgnoreRoute("{resource}.axd/{*pathInfo}");

    // Api - Search methods.
    routes.MapRoute(
        "Search Methods",
        "{controller}/{action}"
    );

In fact, the action method is never called because it doesn't seem to find it :(

But, if create and action without any of those arguments, then it finds it ?????????

How would you write a route and action method to accept some require and some optional arguments, for the route /review/create ?

A: 

As far as i can see you may rewrite your controller action like this:

public ActionResult Create(int foo, int bar, byte blah, string name, int? xxx) {
    // code here
}

The ModelBinder will then ensure that foo,bar and blah are set. Name and xxx may be null. I can't test it a the moment, but i think return type of the action should be ActionResult.

Claus Trojahn
Yep .. that's fine .. i've tried that also .. but i'm not sure how to test it. Unit testing works 100%. Fiddler testing/manual browser POST testing doesn't ... unless i manually make an html page with a form submit, which i don't.
Pure.Krome
Hmm ... never used Fiddler, so i can't say much about that. Perhaps there's some header missing, which is used by binding or asp.net?
Claus Trojahn
or much simpler - Fiddler posts Utf8 encoded?
Claus Trojahn
do you mean, i should check if Fiddler does HTTP-POST with UTF8?
Pure.Krome
jepp, as far as i know, asp.net defaults to recieve utf8 encoded posts. I did a quick check with Firebug which tells me that the form post is `Content-Type : application/x-www-form-urlencoded; charset=UTF-8`.
Claus Trojahn
ok :) i'll check to see if fiddler does the post as UTF8....
Pure.Krome
Claus, have you had any luck with this? the only way i've been able to get this to work is when I have no method paramters and use Request.Form["foo"] .. etc.. to retrieve the values :(
Pure.Krome
I have used a php http class to post external requests to an asp.net mvc project. There have not been any problems with this. But i don't have much experience with external connections and mostly i use get requests in my projects.
Claus Trojahn
A: 

If you are POST'ing a form, just make sure that the elements in your form (textboxes, checkboxes, textarea, etc) have id's that match the parameters in your method. As an alternative you can pass a FormCollection to the method, and do myFormCollection["foo"] to get a string representation of the value (which can then be parsed to an int).

Terje
yep - that is also true BUT i don't want to use an html form page, because that's too much effort. I was hoping to use an application list FIDDLER that allows to me raw POST submit testing stuff...
Pure.Krome
A: 

From my experience, you are missing a number of key elements and concepts with this question.

First and foremost, I don't believe you can execute a POST without a form. The form has to contain the controls from which you pull the values that get passed to the controller method. If the goal is to simply unit test your POST controller method, then just call the method directly in your test, which it appears that you're doing, based on one of your comments. If you involve the view, then you're doing integration testing, not unit testing. Regardless of the test type, the test will always fail because you are choosing not to build the form. Even if you manage to force the POST using Fiddler, Firebug or any other mechanism, you're still not testing the view, you're testing the HTTP protocol.

I highly recommend that you employ a web application testing tool, such as WatiN or Selenium, to test your web pages, rather than throw together a quick-and-dirty test that really doesn't test anything useful.

Neil T.
it's actually an API - just like Twitter's API, etc - so there is no view(s).
Pure.Krome
Also, to execute a post without a Form, you just need to make sure the Content-Type header value for the Request, exists (and contains the correct value:=> `Content-Type : application/x-www-form-urlencoded;` (Cheers to Claus Trojahn comments, in this thread for this answer).
Pure.Krome
A: 

In your post request set content-type="application/json; charset=UTF-8" and pass the values for the method parameter in JSON format. This should make Asp.MVC not to look in FormCollection for those values.

iaimtomisbehave
but that is also not what i want :( i need them to be a form post, still :(
Pure.Krome
iaimtomisbehave