【问题标题】:How to translate query paramters using Attribute Routing in MVC website?如何在 MVC 网站中使用属性路由翻译查询参数?
【发布时间】:2013-04-01 11:27:28
【问题描述】:

我有一个 HorseController 及其 Index 方法,例如:

 [GET("Index", TranslationKey = "HorseIndex")]
    [AllowAnonymous]
    public ActionResult Index(int? page, HorseTypes? type, HorseGenders? gender, HorseBreeds? breed, HorseJumps? jump,
        HorseDressages? dressage, String maxAge, String priceFrom, String priceTo, Country? country, bool? hasPicture, bool? hasVideo)
        {
...
}

我把这条路线翻译成这样:

 provider.AddTranslations()
       .ForKey("HorseIndex", new Dictionary<string, string>
            {
                { "da", "heste-til-salg" },
                { "en", "horses-for-sale" }
            })

问题是,现在丹麦用户的网址是:

http://localhost:12623/heste-til-salg?page=1&hasPicture=False&hasVideo=False

我的问题也是如何翻译查询参数?

我在documentation 中找不到这个。

【问题讨论】:

    标签: asp.net-mvc attributerouting


    【解决方案1】:

    我不认为你可以。 MVC 路由中的查询字符串被视为传递的任何不适合 URL 的数据的通用转储场。如果它不是您在设计时不知道的通用数据,那么它应该只是您的 URL 的一部分。如果它是通用数据,那么显然无法为可能出现在查询字符串中的任何和所有无关数据提供翻译。

    【讨论】:

      【解决方案2】:

      您可以接受已翻译的参数,并在控制器方法中使用 StringsLocalized 进行翻译。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2021-12-12
        • 2018-09-02
        • 1970-01-01
        • 2015-09-04
        • 2013-05-14
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多