在ASP.NET MVC小写的网址 [英] Lower case URLs in ASP.NET MVC

查看:93
本文介绍了在ASP.NET MVC小写的网址的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

是否有可能迫使/扩展路由引擎生成URL小写,给 /控制器/动作而不是 /控制器/动作

Is it possible to force/extend the routing engine to generate URLs in lower case, giving /controller/action instead of /Controller/Action?

推荐答案

更​​重要的是,你要强迫被大写为任何传入请求的重定向应用于小写版本。搜索引擎的网址对待大小写敏感的,这意味着如果你有多个链接到相同的内容,这些内容的网页排名分布,从而稀释。

What's more, you should force any incoming requests that are uppercase to be redirected to the lowercase version. Search engines treat URLs case-sensitively, meaning that if you have multiple links to the same content, that content's page ranking is distributed and hence diluted.

返回HTTP 301(永久移动)这样的链接会导致搜索引擎合并这些链接,因此只能容纳一个引用您的内容。

Returning HTTP 301 (Moved Permanently) for such links will cause search engines to 'merge' these links and hence only hold one reference to your content.

添加像这样到你的的Global.asax.cs 文件:

Add something like this to your Global.asax.cs file:

protected void Application_BeginRequest(object sender, EventArgs e)
{
    // Don't rewrite requests for content (.png, .css) or scripts (.js)
    if (Request.Url.AbsolutePath.Contains("/Content/") ||
        Request.Url.AbsolutePath.Contains("/Scripts/"))
        return;

    // If uppercase chars exist, redirect to a lowercase version
    var url = Request.Url.ToString();
    if (Regex.IsMatch(url, @"[A-Z]"))
    {
        Response.Clear();
        Response.Status = "301 Moved Permanently";
        Response.StatusCode = (int)HttpStatusCode.MovedPermanently;
        Response.AddHeader("Location", url.ToLower());
        Response.End();
    }
}

这篇关于在ASP.NET MVC小写的网址的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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