Search code examples
asp.net-mvc-3routesmaproute

route the url in MVC


I have this path

In view

  <img src="PhotoDisplay.ashx?photoid=12&size=medium" alt="Image with no resize"/>

I want to route the source url to an action . I must need to use this url format because i am rebuilding a site .so i want to make all the links alive in new site too. In global.asax i added the rout like shown below

routes.MapRoute(
          "PhotoDisplay", "PhotoDisplay.ashx?photoid={photoID}&size={size}",
          new { controller = "Images", action = "PhotoDisplay", photoid= "",size="" }
      );

But its giving error while compiling .How can i map route for that kind of url


Solution

  • Query string parameters should not be defined in a route. Try like this:

    routes.MapRoute(
        "PhotoDisplay", 
        "PhotoDisplay.ashx",
        new { controller = "Images", action = "PhotoDisplay" }
    );
    

    and the controller action:

    public ActionResult PhotoDisplay(string photoid, string size)
    {
        ...
    }