简体   繁体   中英

Azure Mobile Services client pass parameters to .NET Backend

I'm developing an Azure Mobile Service with a .NET Backend and the client application that calls this service.

The service has some parameters on the GetAll Method.

 public IQueryable<Appointment> GetAllAppointment(DateTime start, DateTime end)
 {
    // TODO: Handle parameters
    return Query();
 }

The client api has the possibility to pass parameters:

Dictionary<string, string> parameters = new Dictionary<string, string>();
parameters.Add("start", start.ToString());
parameters.Add("end", end.ToString());           

var query = Appointments.WithParameters(parameters);                

var results = await query.ToEnumerableAsync();
return results;

The method on the service is never called. When I remove the parameters on the service method, the method is being called. The Request contains the parameters on the Querystring.

How should I handle parameters correct?

You shouldn't use the default ToString method to convert a DateTime to a string - that will likely cause globalization problems. For example, if you run this code in an en-US culture (the same as the one on the server), it should work fine:

var parameters = new Dictionary<string, string>();
var start = new DateTime(2014, 4, 16, 0, 0, 0, DateTimeKind.Utc);
var end = new DateTime(2014, 6, 19, 0, 0, 0, DateTimeKind.Utc);
parameters.Add("start", start.ToString());
parameters.Add("end", end.ToString());
var t = MobileService.GetTable<TodoItem>();
var items = t.WithParameters(parameters).ToListAsync().Result;
Console.WriteLine(string.Join(", ", items));

That will cause the following request to be sent to the server (line breaks added and URI unescaped for readability):

GET /tables/TodoItem?
    start=4/16/2014 12:00:00 AM
     &end=6/19/2014 12:00:00 AM HTTP/1.1

Now, if you run it on a locale where the date format is dd/MM/yyyy , then the request will be sent as below, and those values do not map to DateTime (there's no month 16 or 19).

GET /tables/TodoItem?
    start=16/4/2014 12:00:00 AM
     &end=19/6/2014 12:00:00 AM HTTP/1.1

What you can do is to use an universal format (such as ISO 8601), so that regardless of the culture of the client, the server will always receive the date in a format it understands. The code below shows an example of this.

Thread.CurrentThread.CurrentCulture = CultureInfo.GetCultureInfo("pt-BR");
Thread.CurrentThread.CurrentUICulture = CultureInfo.GetCultureInfo("pt-BR");
var isoFormat = "yyyy-MM-dd'T'HH:mm:ss.fff'Z'";
var parameters = new Dictionary<string, string>();
var start = new DateTime(2014, 4, 16, 0, 0, 0, DateTimeKind.Utc);
var end = new DateTime(2014, 6, 19, 0, 0, 0, DateTimeKind.Utc);
parameters.Add("start", start.ToString(isoFormat, CultureInfo.InvariantCulture));
parameters.Add("end", end.ToString(isoFormat, CultureInfo.InvariantCulture));
var t = MobileService.GetTable<TodoItem>();
var items = t.WithParameters(parameters).ToListAsync().Result;
Console.WriteLine(string.Join(", ", items));

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM