0

I am using .Net Framework 4.5 and MVC4 with Entity Framework.

I currently have the following code in my RouteConfig.cs:

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

But we now have a page anchor in the About Us page. When a user types in /job_openings.aspx, I want them to be taken right down to that anchor. Does anyone know how to do that?

I've tried doing a MapPageRoute, but that doesn't seem to work:

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

This is the URL I am trying to get to: ~/AboutUs#News from this one: job_opening.aspx

Is this possible?

MikeSmithDev
  • 15,731
  • 4
  • 58
  • 89
Termato
  • 1,556
  • 1
  • 16
  • 33

1 Answers1

0

Sooo thanks to this amazing post here: ASP.NET MVC Redirect to action with anchor

I was actually able to get this to work...BUT I had to do some...finagling.

In the Route config I added the id of 1 so I could handle it in the controller:

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

This is what I did in my controller to handle this request (Using what that one page provided):

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

Now here is the tricky part I had to bang my head on for a while. For some reason on the redirect, it literally went to the route config and took the first item that had controller = "AboutUs", action = "Index" and went to it. So I was in an infinite loop.

So I added this ON TOP of the "Jobs" route:

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

Once I did that, it actually worked!

Community
  • 1
  • 1
Termato
  • 1,556
  • 1
  • 16
  • 33