.net - How to correctly canonicalize a URL in an ASP.NET MVC application? -


I'm trying to find a good general purpose path to canonicalize URLs in an ASP.NET MVC 2 application . This is how I have come so far:

  // Using an authorization filter because it is executed earlier than other filters Public class CanonicalizeAttribute: AuthorizeAttribute {public bool ForceLowerCase { Get; Set; } Public CanonicalizeAttribute (): base () {ForceLowerCase = true; } Public override zero onofilization (authorizationcontent filtercontact) {RouteVulation value = Extrotout value (filtercontax); String CanonicLural = New URL Helper (filterContext.RequestContext) .Root URL (value); If (ForceLover Cas) CanonicLRL = CanonicalLLLTLower (); If (filterContext.HttpContext.Request.Url.PathAndQuery! = CanonicalUrl) filterContext.Result = New Permanent Redirect Result (CanonicalLural); } Private Static RootValueActroit ExtraRightWithOld (Authorization Contact Filter Contains) {var value = filter consort. Root data value. Union (Filters Consort. RouterDataDataToken) .Oxized (X = & gt; x, x => x.will); Var queryString = filterContext.HttpContext.Request.QueryString; Foreach (string key in queryString.Keys) {if (values.ContainsKey (key)) values.Add (key, queryString [key]); } Return the new RouteValueDictionary (value); }} // Redirected results that are permanent (301) public square permanent redirect result: redirect result {redirects to the public permanent Redirect Result (string URL): Base (URL) {} executed without public override (control reference reference) { Context.HttpContext.Response.RedirectPermanent (this .url); }}  

Now I can mark my controllers like this:

  [Canonical] Public Class Home Controller: Controller {/ * ... * /}  

All this seems to work very well, but I have the following worries:

  1. Let me know now Even the canonical object attribute I want to be canolaised when every controller (or action method) thinks about such a situation, Wherever I do not want this behavior, it seems that instead of controlling one at a time, this behavior should be a way to achieve site-wide.

  2. The fact is that I'm applying force to reduce the 'case rules' seems wrong in the filter. Certainly this route is in the argument of url in some way I would not think of a way to do this in my routing configuration, I can not see the controller and the action parameters (as well as any other string root parameters) in the @ "[az] *" Thought about adding obstacles, but I It seems that the routes will not be matched by this. Also, because lower-case rules are not being implemented at the street level, it is possible to generate links in my pages, which are upper-case letters in them, which seem very bad.

Is there something obvious that I'm ignoring here?

Let me tell you about the same "itching", the simple nature of default ASP.NET MVC routing, letter wrapper Pointing to, except slash, etc. I want a general solution to this problem, preferably as part of routing logic in my applications.

After searching the web and higher after searching, after finding no useful library, I decided to make myself a roll. As a result, there is an open source class library that supplements the ASPNET routing engine.

You can install the library through the newgate: Install-Package Canonicalize

and in your route registration: route. Caninelize (). Lowercase ();

In addition to lowercase, many other URLs in the package have been incorporated into canonical strategies. Apply Force www domain prefix on or off, a specific host name, a trailing slash, etc. It is also easy to add custom URL canalicalization strategies, and I would like to open a much more open "official" Canonicalize distribution to accept more strategies for distribution.

I hope you or any other person will be able to find this assistant, even if the question is one year :)


Comments