How can I avoid duplicate content in ASP.NET MVC due to case-insensitive URLs and defaults?

前端 未结 7 1163
无人及你
无人及你 2020-12-24 03:22

Edit: Now I need to solve this problem for real, I did a little more investigation and came up with a number of things to reduce duplicat

7条回答
  •  無奈伤痛
    2020-12-24 03:57

    Like you, I had the same question; except I was unwilling to settle for an all-lowercase URL limitation, and did not like the canonical approach either (well, it's good but not on its own).

    I could not find a solution, so we wrote and open-sourced a redirect class.

    Using it is easy enough: each GET method in the controller classes needs to add just this one line at the start:

    Seo.SeoRedirect(this);
    

    The SEO rewrite class automatically uses C# 5.0's Caller Info attributes to do the heavy lifting, making the code above strictly copy-and-paste.

    As I mention in the linked SO Q&A, I'm working on a way to get this converted to an attribute, but for now, it gets the job done.

    The code will force one case for the URL. The case will be the same as the name of the controller's method - you choose if you want all caps, all lower, or a mix of both (CamelCase is good for URLs). It'll issue 301 redirects for case-insensitive matches, and caches the results in memory for best performance. It'll also redirect trailing backslashes (enforced for index listings, enforced off otherwise) and remove duplicate content accessed via the default method name (Index in a stock ASP.NET MVC app).

提交回复
热议问题