0

I have a controller that looks like this:

public class PageController : Controller
{
    public ActionResult Render(string url)
    {
        //this is just for testing!
        return Content("url was " + url);
    }
}

I'm trying to pass in the value of the url into the controller. For example:
http://www.site.com/products/something/else

Would pass "products/something/else" into my Render action of the PageController.

This is because we are using "products/something/else" as a unique key for a record in the database (legacy system, don't ask)

So, my resultant query would be something along the lines of this:

select * from foo where urlKey = 'products/something/else'

So far I have this in my RegisterRoutes section on Global.asax:

routes.MapRoute("pages", "{*url}", new { controller = "Page", action = "Render", url="/" });

But this isn't working as expected...

By visiting www.site.com/products/something/else, the value passed into the controller is "home/index/0"
The only route defined in RegisterRoutes is that described in the question.

6
  • What isn't working as expected? What is expected? What is the actual result? Commented Jun 20, 2012 at 12:01
  • Edited to answer your questions Commented Jun 20, 2012 at 12:06
  • 1
    I cannot reproduce the issue you are describing. I have created a new ASP.NET MVC application, defined the PageController exactly as shown in your question, the RegisterRoutes exactly as shown in your question and when I hit F5, I see: url was products/something/else. Could you please provide detailed instructions on how we could reproduce your problem? Commented Jun 20, 2012 at 12:10
  • The route works for me too.. I would have checked before answering the question :( Commented Jun 20, 2012 at 12:30
  • @DarinDimitrov @Mark For me, it spits out url was home/index/0 Commented Jun 21, 2012 at 10:21

2 Answers 2

3

The below class matches every route but you can modify as per your needs.

public class LegacyRoute : RouteBase
{
    public override RouteData GetRouteData(HttpContextBase httpContext)
    {
      RouteData result = null;

      string url = httpContext.Request.RawUrl.Substring(1);

      result = new RouteData(this, new MvcRouteHandler());
      result.Values.Add("controller", "Page");
      result.Values.Add("action", "Render");
      result.Values.Add("url", url);

      return result;
    }

    public override VirtualPathData GetVirtualPath(RequestContext requestContext, RouteValueDictionary values)
    {
      return null;
    }
}

In Global.asax.cs

routes.Add(new LegacyRoute());
Sign up to request clarification or add additional context in comments.

Comments

0

Hope this helps, one of our routes does something similar and this is the code:

   routes.MapRoute(
            name: "Standard",
            url: "{controller}/{action}/{id}",
            defaults: new { id = UrlParameter.Optional, action = ControllersAndActions.TypicalController.IndexAction, page = 1 },
            constraints: new
            {
                controller = ControllersAndActions.ControllerConstraintExpression
            }
        );

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.