【问题标题】:RouteConfig & culture in MVC5MVC5 中的 RouteConfig 和文化
【发布时间】:2020-03-06 23:25:53
【问题描述】:

我正在尝试两种不同的方法来在我的多语言网站中设置路线。

使用 RouteConfig.cs,以下两种语言都可以正常工作:

    route = routes.MapRoute(
      name: "DataVesselRegister",
      url: "{culture}/Data/staff",
      defaults: new { action = "Index", controller = "Staff", culture = defaultCulture.Name },
      constraints: new { culture = new SiteCultureConstraint(AppConfig.Sitename) }
    );
    // A route value determines the culture of the current thread
    route.RouteHandler = new MultiCultureMvcRouteHandler();

但是,当我尝试使用 Route 属性来做同样的事情时:

  [Route("{culture}/data/staff")]
        [HttpGet]
        public ActionResult Index()
        {

该网站仍以英语作为其默认语言。

为什么我不能在控制器中使用路由属性实现相同的行为。

【问题讨论】:

    标签: asp.net-mvc routes


    【解决方案1】:

    操作方法签名不包括文化变量。您必须更改您的操作方法以从 URL 中传递变量。

    public ActionResult Index(SiteCultureTypeName culture)
    {
      ...
    }
    

    【讨论】:

    • 试过了,但我收到了关于 SiteCultureTypeName 无法识别的错误。我需要安装一些软件包吗?
    • 该类型将与第一个代码 sn-p 中的“默认值”中的 defaultCulture 类型相同。 SiteCultureTypeName 只是一个占位符。
    猜你喜欢
    • 1970-01-01
    • 2021-10-07
    • 2019-09-13
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-12-31
    • 1970-01-01
    相关资源
    最近更新 更多