Mvcsitemapprovider多路径的单页 [英] Mvcsitemapprovider Multiple paths to the single page

查看:433
本文介绍了Mvcsitemapprovider多路径的单页的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我要让breadcrump导航3路到同一页面。网站地图是在底部。

 < mvcSiteMapNode标题=新控制器=操作行动=NewActions面积=推广>
  < mvcSiteMapNode标题=行动控制器=操作行动=ActionTabDetails面积=推广/>
< / mvcSiteMapNode>
< mvcSiteMapNode标题=继续控制器=操作行动=ContinuesActions面积=推广>
  < mvcSiteMapNode标题=行动控制器=操作行动=ActionTabDetails面积=推广actionStatus =1/>
< / mvcSiteMapNode>
< mvcSiteMapNode标题=完成控制器=操作行动=FinishedActions面积=推广>
  < mvcSiteMapNode标题=行动控制器=操作行动=ActionTabDetails面积=推广actionStatus =2/>
< / mvcSiteMapNode>

我试着用atributes类型和按键,但它并没有帮助。当我打开动作ActionDetails每次,breadcrump样子
根>成品>动作

路径的选择中的控制器确定,通过模型的状态。我现在我应该写自己DynamicNodeProvider但我不知道如何参数传递到从CONTROLER提供商。我不能在底部用行动注解,becouse我现在模型的条件在行动的身体像:

 公众的ActionResult ActionTabDetails(GUID actionTabGuid)
{
    ActionTab模型= actionTabRepo.Get(actionTabGuid,行动小组);    如果(model.Status == ActionStatus.New)
    {
          //参数与我想传递给DynamicNodeProvider或选择当前节点
    }
    //另一个条件
    返回视图(ActionTab / ActionTabDetails模型);
}

我试过:

  SiteMap.CurrentNode = SiteMap.Provider.FindSiteMapNodeFromKey(新);

但在控制器只吸气。

我会的帮助非常gratefull。

更新:

我maked与可选参数的动作:

 公众的ActionResult ActionTabDetails(GUID actionTabGuid,INT actionStatus = 0)

但每一个URL像


  • /推广/操作/动作?actionTabGuid = 822ed729-8edd-4301-970a-867d2b4f9246

  • /Promotion/Actions/Action?actionTabGuid=822ed729-8edd-4301-970a-867d2b4f9246&actionStatus=1

  • /Promotion/Actions/Action?actionTabGuid=822ed729-8edd-4301-970a-867d2b4f9246&actionStatus=2

引导到无参数的第一个节点。我试着在行动obvers参数,但仍然效果是一样的。我会gratefull寻求帮助。


解决方案

要使它工作的关键是要知道,每一个设定的路线值必须在地图中是唯一的。也就是说,你需要另一个参数添加到所有,但路线,无论是参数名称或价值的1必须是每个节点上的不同。

 < mvcSiteMapNode标题=新控制器=操作行动=NewActions面积=推广>
  < mvcSiteMapNode标题=行动控制器=操作行动=ActionTabDetails面积=推广/>
< / mvcSiteMapNode>
< mvcSiteMapNode标题=继续控制器=操作行动=ContinuesActions面积=推广>
  < mvcSiteMapNode标题=行动控制器=操作行动=ActionTabDetails面积=推广someParameter =1/>
< / mvcSiteMapNode>
< mvcSiteMapNode标题=完成控制器=操作行动=FinishedActions面积=推广>
  < mvcSiteMapNode标题=行动控制器=操作行动=ActionTabDetails面积=推广someParameter =2/>
< / mvcSiteMapNode>

路线值的组合(或显式URL上的URL属性设置)为节点是如何被确定为当前节点和第一场比赛总是获胜。但是,如果你增加额外的数据,那么每个节点将是独一无二的。

如果您使用的是默认的路由,你的网址,随后将是这样的:


  • /推广/操作/动作

  • /推广/操作/动作?someParameter = 1

  • /推广/操作/动作?someParameter = 2

请注意,您也可以通过的继承RouteBase 或为了使添加自定义参数到路由的URL更加人性化。

一旦你有你的URL设置您preFER(也就是唯一的网址),则可以使用规范的标签HTML辅助,以确保只有主的网址是由搜索引擎和索引的方式其他被忽略。你只需要要么canonicalKey或CANONICALURL属性设置为主节点。

 < mvcSiteMapNode标题=新控制器=操作行动=NewActions面积=推广>
  < mvcSiteMapNode标题=行动控制器=操作行动=ActionTabDetails面积=推广键=TheMainAction/>
< / mvcSiteMapNode>
< mvcSiteMapNode标题=继续控制器=操作行动=ContinuesActions面积=推广>
  < mvcSiteMapNode标题=行动控制器=操作行动=ActionTabDetails面积=推广someParameter =1canonicalKey =TheMainAction/>
< / mvcSiteMapNode>
< mvcSiteMapNode标题=完成控制器=操作行动=FinishedActions面积=推广>
  < mvcSiteMapNode标题=行动控制器=操作行动=ActionTabDetails面积=推广someParameter =2canonicalKey =TheMainAction/>
< / mvcSiteMapNode>

然后,所有你需要做的添加 @ Html.MvcSiteMap()。CanonicalTag() HTML帮手,在你的页面布局HEAD部分和规范的URL会上备用页面自动创建(而不是主之一)。

请参阅这个帖子为一个可下载的例子。此外,<一个href=\"http://www.shiningtreasures.com/post/2013/09/02/how-to-make-mvcsitemapprovider-remember-a-user-position\"相对=nofollow>这个帖子进入深度有关节点匹配功能是如何工作的。

MVC路由

请记住它是确定如何将URL构建的路线。看看你的RouteConfig.cs文件。

 公共类RouteConfig
{
    公共静态无效的RegisterRoutes(RouteCollection路线)
    {
        routes.IgnoreRoute({}资源个.axd / {*} PATHINFO);        routes.MapRoute(
            名称:默认,
            网址:{控制器} / {行动} / {ID}
            默认:新{控制器=家,行动=索引,ID = UrlParameter.Optional}
        );
    }
}

正如你可以看到,默认路由仅使用ID作为参数。任何你添加不控制器,动作,或ID将成为查询字符串的一部分。您可以添加其他路由,参数和约束作为您的应用需要(具体路由的默认路由属于之前,通常应该保留默认路由)。在路由的深入报道看看 MSDN 获得或谷歌MVC路由,你会发现很多伟大的教程。


  

提示: AttributeRouting 可以很容易地通过几个路线属性装饰它提供了多种途径来操作方法。


  [路线(新动作/动作片的细节/ {} actionTabGuid)]
[路线(不断-动作/动作片的细节/ {} actionTabGuid)]
[路线(成品动作/动作片的细节/ {} actionTabGuid)]
公众的ActionResult ActionTabDetails(GUID actionTabGuid)
{
    ActionTab模型= actionTabRepo.Get(actionTabGuid,行动小组);    如果(model.Status == ActionStatus.New)
    {
          //参数与我想传递给DynamicNodeProvider或选择当前节点
    }
    //另一个条件
    返回视图(ActionTab / ActionTabDetails模型);
}

如果你不想与路由一塌糊涂,我只想使用默认路由,并使用ID作为您的GUID值(因为大多数的行动只会有一个反正)建议。你并不需要actionStatus添加到你的操作方法,如果它不意味着任何东西到你的应用程序和路线不指定它为所需的值(没有缺省路由)。

MvcSiteMapProvider路由

在此另一侧为MvcSiteMapProvider匹配的节点。当使用自定义路由值(参数),您需要配置MvcSiteMapProvider所以它理解你正在使用自定义参数。您(使用通过添加他们的每一个可能的组合作为一个单独的节点做<一个href=\"https://github.com/maartenba/MvcSiteMapProvider/wiki/Defining-sitemap-nodes-using-IDynamicNodeProvider\"相对=nofollow> IDynamicNodeProvider 或ISiteMapNodeProvider),或者你需要强制的每一个值,以配合preservedRouteParameters一个节点。使用单独的节点为每个值,如果它是重要的所有网页的搜索引擎进行索引。使用preservedRouteParameters如果您的网页主要用于数据录入。通常情况下,使用preservedRouteParameters时,也必须使用SiteMapTitleAttribute和知名度提供商来调整菜单和面包屑的外观。有上下载<两者的这些技术的演示href=\"http://www.shiningtreasures.com/post/2013/09/02/how-to-make-mvcsitemapprovider-remember-a-user-position\"相对=nofollow>这个帖子。

I want to make breadcrump navigate 3 paths to the same page. Site map is at the bottom.

<mvcSiteMapNode title="New" controller="Actions" action="NewActions" area="Promotion">
  <mvcSiteMapNode title="Action" controller="Actions" action="ActionTabDetails" area="Promotion"/>
</mvcSiteMapNode>
<mvcSiteMapNode title="Continues" controller="Actions" action="ContinuesActions" area="Promotion">
  <mvcSiteMapNode title="Action" controller="Actions" action="ActionTabDetails" area="Promotion" actionStatus="1"/>
</mvcSiteMapNode>
<mvcSiteMapNode title="Finished" controller="Actions" action="FinishedActions" area="Promotion">
  <mvcSiteMapNode title="Action" controller="Actions" action="ActionTabDetails" area="Promotion" actionStatus="2"/>
</mvcSiteMapNode>

I tried with atributes "type" and "key" but it didn't help. Everytime when I am opening action ActionDetails, breadcrump looks like root > Finished > Action

Selection of path is determined in controller, by the status of model. I now I should write own DynamicNodeProvider but I don't known how to pass parameter to the Provider from controler. I couldn't use action annotation, becouse I now condition of model in action body like at the bottom:

public ActionResult ActionTabDetails(Guid actionTabGuid)
{
    ActionTab model = actionTabRepo.Get(actionTabGuid, "ActionGroup");

    if (model.Status == ActionStatus.New)
    {
          //Parameter with I want to pass to the DynamicNodeProvider or select current node
    }
    //another conditions
    return View("ActionTab/ActionTabDetails", model);
}

I tried:

SiteMap.CurrentNode = SiteMap.Provider.FindSiteMapNodeFromKey("new");

but in controller is only getter.

I'll be very gratefull for help.

Updated:

I maked action with optional parameter:

public ActionResult ActionTabDetails(Guid actionTabGuid, int actionStatus=0)

but every url like

  • /Promotion/Actions/Action?actionTabGuid=822ed729-8edd-4301-970a-867d2b4f9246
  • /Promotion/Actions/Action?actionTabGuid=822ed729-8edd-4301-970a-867d2b4f9246&actionStatus=1
  • /Promotion/Actions/Action?actionTabGuid=822ed729-8edd-4301-970a-867d2b4f9246&actionStatus=2

direct to the first node without parameter. I've tried with obvers parameters in action, but still effect is the same. I will be gratefull for help.

解决方案

The key to making it work is to know that every set of route values must be unique within the SiteMap. That is, you need to add another parameter to all but 1 of the routes and either the parameter name or its value must be different on every node.

<mvcSiteMapNode title="New" controller="Actions" action="NewActions" area="Promotion">
  <mvcSiteMapNode title="Action" controller="Actions" action="ActionTabDetails" area="Promotion"/>
</mvcSiteMapNode>
<mvcSiteMapNode title="Continues" controller="Actions" action="ContinuesActions" area="Promotion">
  <mvcSiteMapNode title="Action" controller="Actions" action="ActionTabDetails" area="Promotion" someParameter="1"/>
</mvcSiteMapNode>
<mvcSiteMapNode title="Finished" controller="Actions" action="FinishedActions" area="Promotion">
  <mvcSiteMapNode title="Action" controller="Actions" action="ActionTabDetails" area="Promotion" someParameter="2"/>
</mvcSiteMapNode>

The combination of route values (or the explicit URL set on the url attribute) is how the node is identified as being the current node and the first match always wins. But if you add extra data, then each node will be completely unique.

If you are using the default route, your URLs would then look like this:

  • /Promotion/Actions/Action
  • /Promotion/Actions/Action?someParameter=1
  • /Promotion/Actions/Action?someParameter=2

Note that you can also customize the MVC routes if you prefer by inheriting RouteBase or adding the custom parameter to the route in order to make the URL more user-friendly.

Once you have your URLs set up the way you prefer (that is, the UNIQUE URLs) you can use the canonical tag HTML helper to ensure that only the "main" URL is indexed by the search engines and the others are ignored. You just need to set either the canonicalKey or the canonicalUrl attribute to that of the "main" node.

<mvcSiteMapNode title="New" controller="Actions" action="NewActions" area="Promotion">
  <mvcSiteMapNode title="Action" controller="Actions" action="ActionTabDetails" area="Promotion" key="TheMainAction"/>
</mvcSiteMapNode>
<mvcSiteMapNode title="Continues" controller="Actions" action="ContinuesActions" area="Promotion">
  <mvcSiteMapNode title="Action" controller="Actions" action="ActionTabDetails" area="Promotion" someParameter="1" canonicalKey="TheMainAction"/>
</mvcSiteMapNode>
<mvcSiteMapNode title="Finished" controller="Actions" action="FinishedActions" area="Promotion">
  <mvcSiteMapNode title="Action" controller="Actions" action="ActionTabDetails" area="Promotion" someParameter="2" canonicalKey="TheMainAction"/>
</mvcSiteMapNode>

Then all you have to do add the @Html.MvcSiteMap().CanonicalTag() HTML helper to the HEAD section in your layout page and the canonical URLs will be created automatically on the alternate pages (but not the "main" one).

See this post for a downloadable example. Also, this post goes into depth about how the node matching functionality works.

MVC Routing

Keep in mind it is the routes that determine how the URL will be constructed. Have a look at your RouteConfig.cs file.

public class RouteConfig
{
    public static void RegisterRoutes(RouteCollection routes)
    {
        routes.IgnoreRoute("{resource}.axd/{*pathInfo}");

        routes.MapRoute(
            name: "Default",
            url: "{controller}/{action}/{id}",
            defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional }
        );
    }
}

As you can see, the default route only uses "id" as the parameter. Anything you add that is not controller, action, or id will become part of the querystring. You can add additional routes, parameters, and constraints as your application requires (more specific routes belong before the default route, and typically you should leave the default route alone). Have a look at MSDN for in depth coverage of routing or google "mvc routing" and you will find lots of great tutorials.

TIP: AttributeRouting makes it easy to provide multiple routes to an action method by decorating it with several Route attributes.

[Route("new-actions/action-tab-details/{actionTabGuid}")]
[Route("continues-actions/action-tab-details/{actionTabGuid}")]
[Route("finished-actions/action-tab-details/{actionTabGuid}")]
public ActionResult ActionTabDetails(Guid actionTabGuid)
{
    ActionTab model = actionTabRepo.Get(actionTabGuid, "ActionGroup");

    if (model.Status == ActionStatus.New)
    {
          //Parameter with I want to pass to the DynamicNodeProvider or select current node
    }
    //another conditions
    return View("ActionTab/ActionTabDetails", model);
}

If you don't want to mess with routing, I would suggest just using the default route and using "id" as your Guid value (since most actions will only have one anyway). You don't need to add "actionStatus" to your action method if it doesn't mean anything to your application and your route doesn't specify it as a required value (the default route does not).

MvcSiteMapProvider Routing

On the other side of this is the node matching for MvcSiteMapProvider. When using custom route values (parameters), you need to configure MvcSiteMapProvider so it understands you are using custom parameters. You do this by either adding every possible combination of them as a separate node (using IDynamicNodeProvider or ISiteMapNodeProvider) or you will need to force every value to match a single node with preservedRouteParameters. Use individual nodes for each value if it is important that all of the pages are indexed in search engines. Use preservedRouteParameters if your pages are primarily for data entry. Usually, when using preservedRouteParameters you also have to use SiteMapTitleAttribute and a visibility provider to adjust the look of the menu and breadcrumb trail. There are demos of both of these techniques for download on this post.

这篇关于Mvcsitemapprovider多路径的单页的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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