简体   繁体   English

Azure移动服务客户端将参数传递给.NET后端

[英]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. 我正在开发一个带有.NET后端的Azure移动服务以及调用此服务的客户端应用程序。

The service has some parameters on the GetAll Method. 该服务在GetAll方法上有一些参数。

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

The client api has the possibility to pass parameters: 客户端api有可能传递参数:

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. Request包含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. 您不应该使用默认的ToString方法将DateTime转换为字符串 - 这可能会导致全球化问题。 For example, if you run this code in an en-US culture (the same as the one on the server), it should work fine: 例如,如果您在en-US文化中运行此代码(与服务器上的相同),它应该可以正常工作:

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): 这将导致将以下请求发送到服务器(添加了换行符并且URI未转义为可读性):

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). 现在,如果您在日期格式为dd/MM/yyyy的区域设置上运行它,则请求将按如下方式发送,并且这些值不会映射到DateTime (没有月份16或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. 您可以做的是使用通用格式(例如ISO 8601),这样无论客户端的文化如何,服务器将始终以其理解的格式接收日期。 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));

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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