T4MVC Links for SEO

I am trying to switch our links to T4MVC , and I have a little problem with the parameters, t part of the action signature. We have a route that looks something like this:

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

==> leads to BarController.Details (barID).

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

So far we have used

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

And that will lead us to BarController.Details (barID), storing fooKey and barKey in the URL.

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

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

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

?

+3
3

T4MVC ( ). , T4MVC.

, . , T4MVC, ActionResult, .

, . , :

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

, :

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

AddRouteValues:

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;
}

, , .

,

+9

T4MVC.cs. html-, ActionLink. , ActionResult.GetRouteValueDictionary().

    public static MvcHtmlString ActionLink(this HtmlHelper htmlHelper, string linkText, ActionResult result, IDictionary<string, object> htmlAttributes) {
        return htmlHelper.RouteLink(linkText, result.GetRouteValueDictionary(), htmlAttributes);
    }
+1

jfar!

Here is the code I used, just in case someone needs it. It can use refactoring work, but it works

public static MvcHtmlString ActionLink<T>(this HtmlHelper<T> htmlHelper, string linkText, ActionResult result,
                                              object extraRouteValues, object htmlAttributes)
    {
        RouteValueDictionary completeRouteValues = result.GetRouteValueDictionary();
        RouteValueDictionary extraRouteValueDictionary = new RouteValueDictionary(extraRouteValues);
        foreach (KeyValuePair<string, object> foo in extraRouteValueDictionary)
        {
            completeRouteValues.Add(foo.Key, foo.Value);
        }

        Dictionary<string, object> htmlAttributesDictionary = htmlAttributes != null ? htmlAttributes.GetType().GetProperties().ToDictionary(p => p.Name, p => p.GetValue(htmlAttributes, null)) : null;

        return htmlHelper.RouteLink(linkText, completeRouteValues, htmlAttributesDictionary);
    }
-1
source

Source: https://habr.com/ru/post/1725396/


All Articles