简体   繁体   English

如何将时区名称从 Angular 传递到 .NET API?

[英]How to pass TimeZone Name from Angular to .NET API?

I have an API that accepts the TimeZone name (America/Denver) as a string.我有一个 API 接受时区名称(美国/丹佛)作为字符串。 In the Angular UI application, I am passing the TimeZone name by detecting the browser timeZoneName.在 Angular UI 应用程序中,我通过检测浏览器 timeZoneName 来传递 TimeZone 名称。 But when the API receives the string as America/Denver it considers as a route parameter and doesn't work as expected.但是,当 API 接收到America/Denver的字符串时,它会将其视为路由参数并且无法按预期工作。

So, I am replacing the / with - in Angular using string.replace() and passing to the API, and converting it back in .NET API.因此,我使用 string.replace() 将 Angular 中的/替换为-并将其传递给 API,然后将其转换回 .NET16344238D014A1674238D014A1674238A108A3

I think it is a bad practice to modify timeZone names.我认为修改时区名称是一种不好的做法。 Can anyone advise on how to pass timeZone names from Angular to .NET API?谁能建议如何将时区名称从 Angular 传递到 .NET API?

Is it possible to pass the offsets from the UI and get timeZone names in API?是否可以从 UI 传递偏移量并在 API 中获取时区名称?

You would need to encode the string parameter you are sending to the api.您需要对发送到 api 的字符串参数进行编码。

Do something like:执行以下操作:

let timeZone  = "America/Denver"

let encodedTimeZone = encodeURIComponent(timeZone);

and send the encodedTimeZone to the api.并将编码时区发送到encodedTimeZone You can log the encodedTimeZone to the console to see the encoded string.您可以将encodedTimeZone记录到控制台以查看编码字符串。

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

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