You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何禁用/Computer/Search路由,解决ASP.NET路由重复及谷歌索引问题?

解决/Computer/Search路由访问问题的方案

你遇到的问题是因为ASP.NET MVC的默认路由({controller}/{action}/{id})仍会匹配/Computer/Search路径,导致同一内容对应两个可访问URL。以下是几种可行的解决方法:

方法一:给默认路由添加排除约束

  1. 创建自定义路由约束类,用于排除指定控制器和动作:
public class ExcludeRouteConstraint : IRouteConstraint
{
    private readonly string _targetController;
    private readonly string _targetAction;

    public ExcludeRouteConstraint(string controller, string action)
    {
        _targetController = controller;
        _targetAction = action;
    }

    public bool Match(HttpContextBase httpContext, Route route, string parameterName, RouteValueDictionary values, RouteDirection routeDirection)
    {
        var currentController = values["controller"] as string;
        var currentAction = values["action"] as string;

        // 匹配到要排除的控制器和动作时,返回false,不使用该路由
        return !(string.Equals(currentController, _targetController, StringComparison.OrdinalIgnoreCase) &&
                 string.Equals(currentAction, _targetAction, StringComparison.OrdinalIgnoreCase));
    }
}
  1. 修改默认路由,添加该约束:
routes.MapRoute(
    name: "Default",
    url: "{controller}/{action}/{id}",
    defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional },
    constraints: new { controller = new ExcludeRouteConstraint("Computer", "Search") },
    namespaces: new[] { "Pixel.Inforworld.Website.Controllers" }
);

配置完成后,默认路由将不再匹配/Computer/Search,只有你定义的eq路由能访问对应内容。

方法二:添加301永久重定向(推荐,兼顾SEO)

为避免旧URL被搜索引擎收录或用户误访问,可添加路由将/Computer/Search永久重定向到/eq:

  1. 在RouteConfig中新增重定向路由(需放在你的ComputerSearch路由之前):
routes.MapRoute(
    name: "RedirectComputerSearch",
    url: "Computer/Search",
    defaults: new { controller = "Computer", action = "RedirectToEq" },
    namespaces: new[] { "Pixel.Inforworld.Website.Controllers" }
);

// 你的原有自定义路由
routes.MapRoute(
    name: "ComputerSearch",
    url: "eq",
    defaults: new { controller = "Computer", action = "Search", ID = UrlParameter.Optional },
    namespaces: new[] { "Pixel.Inforworld.Website.Controllers" }
);
  1. 在ComputerController中添加重定向动作:
public ActionResult RedirectToEq(string cat)
{
    // 301永久重定向,告知搜索引擎URL已永久变更
    return RedirectPermanent($"~/eq?cat={cat}");
}

注意事项

  • 路由顺序至关重要:重定向路由和自定义路由需放在默认路由之前,确保优先匹配。
  • 301重定向是SEO友好的方案,能引导搜索引擎更新索引,彻底解决重复内容问题。

内容的提问来源于stack exchange,提问作者delphiman

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.21 14:57:27