SEO的T4MVC链接 [英] T4MVC Links for SEO

查看:142
本文介绍了SEO的T4MVC链接的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试将链接切换到 T4MVC ,然后我m对于不是动作签名一部分的参数有一个小问题.我们有一条路线是这样的:

I'm trying to switch our links over to T4MVC, and I'm having a small problem with parameters that aren't part of an action's signature. We have a route that goes something like this:

http://www.mydomain.com/{fooKey}/{barKey}/{barID}

==>导致 BarController.Details(barID).

fooKey和barKey仅添加到用于SEO的链接中. (因为bar是foo的子实体,我们希望在URL中表示该层次结构)

fooKey and barKey are only added to the links for SEO purposes. (since bar is a child entity of foo, and we want to represent that hierarchy in the URL)

到目前为止,我们将使用

Up until now, we would use

<% =Html.ActionLink(bar.Name, "Details", "Bar", new {barID = bar.ID, fooKey = bar.Foo.Key, barKey = bar.Key}, null)%>

这将导致我们进入BarController.Details(barID),同时将fooKey和barKey保留在URL中.

And that would lead us to BarController.Details(barID), while keeping fooKey and barKey in the URL.

现在我们从T4MVC开始,我们尝试将其更改为

Now that we started with T4MVC, we tried changing it to

<% =Html.ActionLink(bar.Name, MVC.Bar.Details(bar.ID), null)%>

由于barKey和fooKey不属于Details动作签名的一部分,因此它们在URL中不再可见.

Since barKey and fooKey are not part of the Details action signature, they are no longer visible in the URL.

是否有解决此问题的方法,而不必将这些参数添加到动作签名中?

Is there a way around this without having to add these parameters to the action signature?

推荐答案

T4MVC论坛上也出现了类似的问题(

Similar thing also came up on the T4MVC Forum (this thread). I think I'll go ahead and add support for it in T4MVC.

实际上,我只是想出一种解决此问题的有趣方法.添加重载以传递额外的参数的问题是,您需要为所有其他采用ActionResult的T4MVC扩展方法添加类似的重载,这可能会造成混乱.

Actually, I just thought of an interesting way to solve this. The problem with adding an overload to pass extra arguments is that you'd need to add similar overloads to all the other T4MVC extension methods that take an ActionResult, which can get messy.

相反,我们可以使用流利的方法,毫不费力地将其提供给任何地方.这个想法是,您将编写:

Instead, we can use a fluent approach to make this available everywhere with little effort. The idea is that you'll write:

<%= Html.ActionLink(
    bar.Name,
    MVC.Bar.Details(bar.ID)
        .AddRouteValues(new {fooKey = bar.Foo.Key, barKey = bar.Key}))%>

或者如果您只需要添加一个值:

Or if you only needed to add one value:

<%= Html.ActionLink(
    bar.Name,
    MVC.Bar.Details(bar.ID)
        .AddRouteValue("fooKey", bar.Foo.Key))%>

以下是实现AddRouteValues的方法:

Here is how AddRouteValues is implemented:

public static ActionResult AddRouteValues(this ActionResult result, object routeValues) {
    return result.AddRouteValues(new RouteValueDictionary(routeValues));
}

public static ActionResult AddRouteValues(this ActionResult result, RouteValueDictionary routeValues) {
    RouteValueDictionary currentRouteValues = result.GetRouteValueDictionary();

    // Add all the extra values
    foreach (var pair in routeValues) {
        currentRouteValues.Add(pair.Key, pair.Value);
    }

    return result;
}

public static ActionResult AddRouteValue(this ActionResult result, string name, object value) {
    RouteValueDictionary routeValues = result.GetRouteValueDictionary();
    routeValues.Add(name, value);
    return result;
}

如果您可以尝试一下并让我知道如何使用它,那将是很棒的事情.

It would be great if you could give this a try and let me know how that works for you.

谢谢, 大卫

这篇关于SEO的T4MVC链接的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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