views:

849

answers:

2

Given the following code:

using (var client = new WebClient())
  {
    string url = string.Concat(someUrl, "SomeControllerName/", currentId, "/WriteLogFile");
    var toWrite = DateTime.Now

    /* Code to post object to URL goes here e.g. client.UploadValues(url, someNameValueCollectionObject)*/
  }

And the controller method signature:

public ActionResult WriteLogFile(DateTime date, int id)

How can I make the first portion of the code pass the DateTime object to this ActionResult method?

A: 

Add a query string parameter:

var toWrite = DateTime.Now;
string url = string.Concat(someUrl, "SomeControllerName/", currentId, "/WriteLogFile");
url = string.Concat(url, "?date=", toWrite.ToString("s"));
Craig Stuntz
That's the solution that I currently have at the moment. I convert each object to its string representation.
Draco
Well, no matter what you'll have to convert to a string representation, since you're making an HTTP request. If you don't have a route which includes a "date" parameter, you'll have to use the QueryString as Craig suggests.
anurse
Well, that's the only way to do it. Http is plain text: http://blogs.teamb.com/craigstuntz/2009/02/16/38024/ Query strings are text. Form fields are text. Everything that you can pass to the server in an GET request is text.
Craig Stuntz
A: 

you can use the format string in for the date

string url = string.Format("someUrl/SomeControllerName/WriteLogFile/{0}/{1}", currentId, DateTime.Now.ToString("MM-dd-yyyy"));

and add an entry in to the routes table to route it to the appropriate controller and action

routes.MapRoute("SomeRoutename",
                "SomeControllerName/WriteLogFile/{id}/{date}",
                new {   controller = "SomeControllerName", action = "WriteLogFile", 
                        date= DateTime.Now});
Rony