在网址Mvc.sitemap多语言菜单,语言 [英] Multilanguage menu for Mvc.sitemap with language in an Url

查看:286
本文介绍了在网址Mvc.sitemap多语言菜单,语言的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的路线:

"{lang}/{controller}/{action}/{id}"

{}郎这里:EN-US,德-DE等

{lang} here is: en-US, de-DE, etc

一切正常,直到权时刻,我想切换语言。当我切换语言,我失去了菜单的一部分。

Everything works right till moment I would like to switch language. When I switch language I am loosing part of the menu.

我MenuHelperModel.chtml

my MenuHelperModel.chtml

@model MvcSiteMapProvider.Web.Html.Models.MenuHelperModel
@using System.Web.Mvc.Html
@using MvcSiteMapProvider.Web.Html.Models
<div class="hmenu">
  <ul class="tabs">
    @foreach (var node in Model.Nodes)
    { 
      <li>@Html.DisplayFor(m => node)</li>
    }
  </ul>
</div>
@foreach (var node in Model.Nodes)
{ 
  if (node.IsInCurrentPath)
  {
    if (node.Children.Any()) {
     //Left side menu
     <div class="vmenu" style="float: left;">
       <ul class="tabs">
          @foreach (var child in node.Children)
          { 
            <li><a href="@child.Url">@child.Description</a></li>
          }
        </ul>
      </div>
    }
  }
}

一切只为默认语言工作正确的,对于所有其他node.IsInCurrentPath =>假的。
它看起来像我需要重写它。可能有人给出建议怎么办呢?有没有办法解决这个问题的任何其他方式?或者,也许我需要以某种方式分配父?但它为默认语言。

Everything works correct only for default language, for all others node.IsInCurrentPath => false. It looks like I need to override it. Could somebody give an advice how to do it? Is there any other way to resolve the problem? Or maybe I need to assign a parent somehow? But it works for default language.

推荐答案

您只需要将其设置为<一个href=\"http://www.shiningtreasures.com/post/2013/09/02/how-to-make-mvcsitemapprovider-remember-a-user-position#forcing-a-match\"相对=nofollow>强制的参数匹配所以它总是会从当前的请求转换成的的RouteValues​​字典的路径复制节点。

You just need to set it up to force a match on the lang parameter so it will always be copied over from the route of the current request into the RouteValues dictionary of the nodes.

<mvcSiteMapNode title="Home" controller="Home" action="Index" preservedRouteParameters="lang">
    <mvcSiteMapNode title="About" controller="Home" action="About" preservedRouteParameters="lang"/>
</mvcSiteMapNode>

我怀疑它为什么工作为默认语言的原因,是因为你已经在你的路由设置默认参数。但在技术上,它仍然不能匹配当前URL的路线,因为你没有平衡等式的两边。你必须看起来像这样的值:

I suspect the reason why it is "working" for the default language is because you have set a default lang parameter in your route. But technically, it is still failing to match with the current URL's route because you haven't balanced both sides of the equation. You have values that look like this:

|-----------------------------------|-----------------------------------|
|         Current Request           |          SiteMap Node             |
|-----------------------------------|-----------------------------------|
|      Key       |      Value       |      Key       |      Value       |
|-----------------------------------|-----------------------------------|
| controller     | Home             | controller     | Home             |
| action         | Index            | action         | Index            |
| lang           | de-DE            |                |                  |
|-----------------------------------|-----------------------------------|

这并不因为缺少匹配郎中的节点密钥和价值。添加preservedRouteParameters与相应的按键会随着当前请求将它们复制他们相比前:

Which does not match because of the missing lang key and value in the node. Adding preservedRouteParameters with the appropriate keys will copy them over from the current request before they are compared:

|-----------------------------------|-----------------------------------|
|         Current Request           |          SiteMap Node             |
|-----------------------------------|-----------------------------------|
|      Key       |      Value       |      Key       |      Value       |
|-----------------------------------|-----------------------------------|
| controller     | Home             | controller     | Home             |
| action         | Index            | action         | Index            |
| lang           | de-DE            | lang           | de-DE            |
|-----------------------------------|-----------------------------------|

这将迫使匹配与郎参数的任何值。如果所有其他键和值也匹配,你将不得不为CurrentNode属性,不为空,得到的SiteMapPath和菜单的许多功能的工作是很重要的一个值。它完全是由你来通过使用preservedRouteParameters或配置为每个路由值组合的独立节点书平衡,但始终必须用比面积,控制器和行动等路由值时,应考虑到。

Which will force a match with any value of the lang parameter. If all of the other keys and values also match, you will have a value for the CurrentNode property that is not null, which is important to get the SiteMapPath and many features of the Menu to work. It is completely up to you to balance the books either by using preservedRouteParameters or configuring a separate node for for each route value combination, but this always must be taken into consideration when using route values other than area, controller, and action.

此外,请参阅这个答案了解有关设置的SiteMapNode属性定位。

Also, see this answer for information about setting up localization of SiteMapNode properties.

这篇关于在网址Mvc.sitemap多语言菜单,语言的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆