【问题标题】:Redirect to a page with HTML Anchor in RouteConfig在 RouteConfig 中重定向到带有 HTML Anchor 的页面
【发布时间】:2014-03-13 17:51:40
【问题描述】:

我将 .Net Framework 4.5 和 MVC4 与 Entity Framework 一起使用。

我的 RouteConfig.cs 目前有以下代码:

routes.MapRoute(
    name: "Jobs",
    url: "job_openings.aspx",
    defaults: new { controller = "AboutUs", action = "Index", id = UrlParameter.Optional }
);

但我们现在在“关于我们”页面中有一个页面锚点。当用户输入 /job_openings.aspx 时,我希望他们被带到那个锚点。有人知道怎么做吗?

我尝试过 MapPageRoute,但这似乎不起作用:

routes.MapPageRoute("Jobs", "job_opening.aspx", "~/AboutUs#News");

这是我想要访问的 URL:~/AboutUs#News 来自这个:job_opening.aspx

这可能吗?

【问题讨论】:

    标签: c# asp.net asp.net-mvc asp.net-mvc-4 redirect


    【解决方案1】:

    非常感谢这篇精彩的帖子:ASP.NET MVC Redirect to action with anchor

    我实际上能够让它工作......但我必须做一些......摸索。

    在 Route 配置中,我添加了 1 的 id,以便我可以在控制器中处理它:

    routes.MapRoute(
                   name: "Jobs",
                   url: "job_openings.aspx",
                   defaults: new { controller = "AboutUs", action = "Index", id = 1 }
               );
    

    这是我在控制器中为处理此请求所做的(使用该页面提供的内容):

    public ActionResult Index(int id = 0)
            {
                if (id == 1)
                {
                    return Redirect(Url.RouteUrl(new { controller = "AboutUs", action = "Index" }) + "#News");
                }
                return View();
            }
    

    现在这是我不得不猛烈抨击一段时间的棘手部分。出于某种原因,在重定向时,它确实进入了路由配置并获取了第一个具有 controller = "AboutUs", action = "Index" 的项目并转到它。所以我陷入了无限循环。

    所以我在“工作”路线的顶部添加了这个:

    routes.MapRoute(
                   name: "AboutUs",
                   url: "AboutUs",
                   defaults: new { controller = "AboutUs", action = "Index", id = UrlParameter.Optional }
               );
    

    一旦我这样做了,它就真的奏效了!

    【讨论】:

      猜你喜欢
      • 2013-12-28
      • 2010-11-21
      • 1970-01-01
      • 2019-06-25
      • 2021-12-26
      • 1970-01-01
      • 1970-01-01
      • 2013-05-03
      • 2012-08-09
      相关资源
      最近更新 更多