0

I'm developing an application that will replace a much older one. The older one has been in use for many years, and utilises querystring paramaters to reference certain things, for

http://example.com/view_object.asp?obj=Banana

I'd like to re-direct this to my ObjectController's Index action, while passing the querystring value "Banana", in order to allow the older URLs to continue functioning with the new system.

So, the new URL would be:

http://example.com/Object/Banana

I thought this would be simple, by using routes.MapRoute, but i run into an issue where i can't use the ? character.

How do i go about doing this?

Here is what was tried:

routes.MapRoute(
        name: "OldObjectRoute",
        url: "view_object.asp?obj={object}",
        defaults: new { Controller = "Object", action = "Index", object = UrlParameter.Optional }
        );

EDIT: So, based on the suggestion below that this is a duplicate, i did try what was suggested. I came up with:

routes.MapRoute(
                name: "OldObjectRoute",
                url: "view_object.asp{OldList}",
                defaults: new { Controller = "Object", action = "Index", OldList = UrlParameter.Optional }
                );

In my controller i have an optional string "OldList" now, that is expecting values from this - and adjusts the querystring to remove the query, and return the correct data based on the object that was requested.

However, now it simply does not work - I'm informed by the IIS Web Core "ASPClassic" handler that a 404 occurs when using MapRequestHandler. Sounds like it's now interpretting this as a request for a Classic ASP page? Does anyone know how to stop this happening?

user3012708
  • 793
  • 1
  • 11
  • 33
  • 1
    This is definitely not a duplicate of the linked answer. See [Dots in URL causes 404 with ASP.NET mvc and IIS](https://stackoverflow.com/questions/11728846/dots-in-url-causes-404-with-asp-net-mvc-and-iis) as for why you are getting the error message. For the redirect, you should use a 301 redirect which can be done using the [IIS rewrite module](https://stackoverflow.com/a/46670476/), or by [extending the `RouteBase` class](https://stackoverflow.com/a/36168395/) (since the default routing doesn't handle the query string at all). – NightOwl888 Dec 08 '17 at 21:23

1 Answers1

0

The url argument to the MapRoute method specifies a URL pattern. It means you don't specify the query string inside it, but the expected route values names. So your route mapping should look like this:

    routes.MapRoute(
    name: "OldObjectRoute",
    url: "{controller}/{action}/{object}",
    defaults: new { Controller = "Object", action = "Index", object = UrlParameter.Optional }
    );

But this mapping should be in place already so you might want to distinguish the "old" route, so do:

    routes.MapRoute(
    name: "OldObjectRoute",
    url: "old/{controller}/{action}/{object}",
    defaults: new { Controller = "Object", action = "Index", object = UrlParameter.Optional }
    );
Bozhidar Stoyneff
  • 3,576
  • 1
  • 18
  • 28