简体   繁体   English

ASP 核心中的本地化查询字符串参数

[英]Localized query string parameters in ASP core

I'm having issues with a controller which gets as parameters in the query string a pair of coordinates.我在使用 controller 时遇到问题,它在查询字符串中作为参数获取一对坐标。

My controller looks like:我的 controller 看起来像:

[HttpGet("/location")]
public IActionResult GetForLocation(double lat, double lon)
{
// Do stuff
}

When clients send coordinates with different decimal separators (eg /location?lat=4,4&lon=45,5 ) the framework's parsed them to 44 and 455 .当客户端发送带有不同小数分隔符的坐标(例如/location?lat=4,4&lon=45,5 )时,框架会将它们解析为44455 I've used app.UseRequestLocalization() in my startup class and reviewing the current thread culture I get the correct values for the decimal separators, but the model bindings are wrong.我在我的启动 class 中使用app.UseRequestLocalization()并查看了当前的线程文化,我得到了小数分隔符的正确值,但是 model 绑定是错误的。

Minutes after asking this I found the answer, https://docs.microsoft.com/en-us/aspnet/core/mvc/models/model-binding?view=aspnetcore-5.0#globalization-behavior-of-model-binding-route-data-and-query-strings问完几分钟后,我找到了答案, https://docs.microsoft.com/en-us/aspnet/core/mvc/models/model-binding?view=aspnetcore-5.0#globalization-behavior-of-model-binding -路由数据和查询字符串

In short Query string parameters and Paths are treated by default as Invariant Culture unless you replace the QueryStringValueProviderFactory and RouteValueProviderFactory with the following:简而言之,查询字符串参数和路径默认情况下被视为不变文化,除非您将QueryStringValueProviderFactoryRouteValueProviderFactory替换为以下内容:

    public class CulturedQueryStringValueProviderFactory : IValueProviderFactory
    {
        /// <inheritdoc />
        public Task CreateValueProviderAsync(ValueProviderFactoryContext context)
        {
            if (context == null)
            {
                throw new ArgumentNullException(nameof(context));
            }

            var query = context.ActionContext.HttpContext.Request.Query;

            if (query != null && query.Count > 0)
            {
                var valueProvider = new QueryStringValueProvider(
                    BindingSource.Query,
                    query,
                    CultureInfo.CurrentCulture);

                context.ValueProviders.Add(valueProvider);
            }

            return Task.CompletedTask;
        }
    }
    public class CulturedRouteValueProviderFactory : IValueProviderFactory
    {
        /// <inheritdoc />
        public Task CreateValueProviderAsync(ValueProviderFactoryContext context)
        {
            if (context == null)
            {
                throw new ArgumentNullException(nameof(context));
            }

            var valueProvider = new RouteValueProvider(
                BindingSource.Path,
                context.ActionContext.RouteData.Values,
                CultureInfo.CurrentCulture);

            context.ValueProviders.Add(valueProvider);

            return Task.CompletedTask;
        }
    }

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

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