Felipe Pessoto
Felipe Pessoto

Reputation: 6969

Generating Canonical Url in ASP.NET MVC

I want to generate Canonical url´s at my ASP.NET MVC project. I did a simple test and worked, but is it right? Can I have some problems? I see some examples much more complicated, mine seems too good to be truth

I am just doing this:

public string CanonicalUrl()
{
    RouteValueDictionary valores = new RouteValueDictionary(ViewContext.RouteData.Values);
    foreach (KeyValuePair<string, ModelState> keyValuePair in ViewContext.ViewData.ModelState)
    {
        valores[keyValuePair.Key] = keyValuePair.Value.Value.AttemptedValue;
    }

    return Url.RouteUrl(null, valores, Request.Url.Scheme, null);
}

I am using it now. Until now I didn´t have any problem:

http://blog.fujiy.net/?page=5

http://blog.fujiy.net/?tag=%2Fjavascript&page=5

Upvotes: 0

Views: 2656

Answers (1)

Felipe Pessoto
Felipe Pessoto

Reputation: 6969

It worked

public string CanonicalUrl()
{
    RouteValueDictionary valores = new RouteValueDictionary(ViewContext.RouteData.Values);
    foreach (KeyValuePair<string, ModelState> keyValuePair in ViewContext.ViewData.ModelState)
    {
        valores[keyValuePair.Key] = keyValuePair.Value.Value.AttemptedValue;
    }

    return Url.RouteUrl(null, valores, Request.Url.Scheme, null);
}

Upvotes: 1

Related Questions