views:

32

answers:

1

If another website is making a POST to my ASP.NET MVC 2 site, how can I capture it? Do I need to do anything to the routes?

eg The other site does this:

string url = "https://mvc2-site.com/TestReceive";  // that's my site's controller action

NameValueCollection inputs = new NameValueCollection();
inputs.Add("SessionId", "11111");
System.Net.WebClient Client = new WebClient();
byte[] result = Client.UploadValues(url, inputs);

How do I receive that POST on my site? I have tried:

public ActionResult TestReceive(FormCollection fc) {} // doesn't get hit

public ActionResult TestReceive(string SessionId) {} // method gets hit but session id is null

public ActionResult TestReceive() 
{
    var param = ControllerContext.RouteData.Values["parameter"].ToString(); // Values["parameter"] is null
} 

I do not control the other site and it must be a POST (not a webservice or anything else)

As another twist they will POST around 10 variables, but some are optional, so they may not be in the POST data.

+1  A: 
[HttpPost]
public ActionResult TestReceive(string sessionId) 
{
    ...
}

Also as there's no controller specified in the url you should make sure that the controller that contains this action is the default controller in the routes.

Darin Dimitrov
I tried that one, and sessionId was null - wonder if I did something wrong? But I just retested now and it was null again. I made sure to match the case of the SessionId param correctly too. And the url above was an example, the real one does have /controller/action.
JK
@JK, the case doesn't matter. I've just tested this using the default project template (default routes) and it worked perfectly fine.
Darin Dimitrov
I'm just testing it with a plain html file with < form action="..etc..." method="post"> < input id="SessionId" >. I will have to see if using WebClient makes a difference (it shouldn't)
JK
I hope you are testing with `<input name="SessionId" type="text" value="foo" />` and not `<input id="SessionId" type="text" value="foo" />` as there's a fundamental difference.
Darin Dimitrov
Thanks - the problem was using id="SessionId" instead of name="SessionId" in the test html form
JK