【问题标题】:Cannot get route to rename URL无法获取重命名 URL 的路由
【发布时间】:2013-05-04 09:32:26
【问题描述】:

我正在尝试从我的 URL 中删除“家”,换句话说:

www.domain.com/home/about/ 变为 www.domain.com/aboutus

问题是,房子没有被拆除,我不知道为什么。我can see others have identical questions with near identical answers as to mine here on on SO 所以我不知道为什么这不起作用。

我的 Global.asax 是

using System.Web.Http;
using System.Web.Mvc;
using System.Web.Routing;

namespace Company.Ui
{
    // Note: For instructions on enabling IIS6 or IIS7 classic mode, 
    // visit http://go.microsoft.com/?LinkId=9394801
    public class MvcApplication : System.Web.HttpApplication
    {
        protected void Application_Start()
        {
            AreaRegistration.RegisterAllAreas();

            WebApiConfig.Register(GlobalConfiguration.Configuration);
            FilterConfig.RegisterGlobalFilters(GlobalFilters.Filters);
            RouteConfig.RegisterRoutes(RouteTable.Routes);
        }

        public static void RegisterRoutes(RouteCollection routes)
        {
            routes.IgnoreRoute("{resource}.axd/{*pathInfo}");

            routes.MapRoute("RemoveHomeUrl", // Route name
                "{action}", // URL with parameters
                new { controller = "Home", action = "Index" } // Parameter defaults
            );


            routes.MapRoute(
                "Default", // Route name
                "{controller}/{action}/{id}", // URL with parameters
                new { controller = "Home", action = "Index", id = UrlParameter.Optional } // Parameter defaults
            );

        }
    }
}

我的 ActionLink 代码是:

@Html.ActionLink("About us", "AboutUs", "Home", null, new { @class = "mega" })

当我将鼠标悬停在链接上并单击该链接时,它仍然返回 www.domain.com\home\aboutus

我在 Visual Studio 2012 的调试模式下运行它。

我很茫然,谁能帮忙?

【问题讨论】:

  • 请在您的帖子中显示您的所有路线。你离开Default了吗?你还有其他人吗?
  • @DaveA,我已经进行了更新,我正在展示我的整个 Global.asax 文件。只有 2 条路线。
  • 如果您删除 Default 会发生什么?它仍然使用/Home 渲染吗?
  • 不,同样的问题仍然存在。我什至尝试删除默认值,然后将我的 RemoveHomUrl 重命名为默认值(因此只有 1 条路线),这也没有任何区别。
  • 很奇怪。在大范围内,尝试删除 class 参数,看看它是否正常

标签: asp.net-mvc-4 routes


【解决方案1】:

我认为你在错误的地方处理你的路线,

从显示的代码看来,注册的路由是在RouteConfig 类中定义的

protected void Application_Start()
{
    AreaRegistration.RegisterAllAreas();

    WebApiConfig.Register(GlobalConfiguration.Configuration);
    FilterConfig.RegisterGlobalFilters(GlobalFilters.Filters);
    RouteConfig.RegisterRoutes(RouteTable.Routes); //routes are registered here
}

尝试替换

RouteConfig.RegisterRoutes(RouteTable.Routes);

RegisterRoutes(RouteTable.Routes);

或在RouteConfig 类中编辑

希望这会有所帮助。

【讨论】:

  • 你是对的。我创建了一个新的 MVC4 应用程序,它在 App_Start 文件夹中注册地图(默认情况下在 VS 中)。我不得不将我的代码复制到这个文件中并且它有效。谢谢
猜你喜欢
  • 1970-01-01
  • 2021-06-08
  • 2016-11-03
  • 1970-01-01
  • 2018-12-15
  • 1970-01-01
  • 2014-02-06
  • 1970-01-01
  • 2011-06-28
相关资源
最近更新 更多