0

I've implemented code to encrypt my query string parameter names and values. The code i have implemented will only encrypt query string that contain ?. (This is to prevent encryption of unneeded URL's, such as the .css files).

A way to combat this would be to always show the ? in query strings when only the ID parameter is passed.

For example I would like: http://domain/controller/Action/17
To show as: http://domain/controller/Action/?id=17

I understand that I probably need to edit my routes, I've tried adding the ? symbol to the route which throws the error : The route URL cannot start with a '/' or '~' character and it cannot contain a '?' character.

My routes are defined as:

routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
routes.IgnoreRoute("favicon.ico");

routes.MapRoute(
    "Default", // Route name
    "{controller}/{action}/{id}", // URL with parameters
    new { controller = "Home", action = "Login", id = UrlParameter.Optional } // Parameter defaults
);

How can I get my query strings to show like the example given above?

DNKROZ
  • 2,634
  • 4
  • 25
  • 43
  • Change as below, routes.MapRoute( "Default", // Route name "{controller}/{action}", // URL with parameters new { controller = "Home", action = "Login", id = UrlParameter.Optional } // Parameter defaults ); – Thennarasan Jan 10 '18 at 09:04
  • Refer https://stackoverflow.com/questions/12619587/routing-based-on-query-string-parameter-name – Thennarasan Jan 10 '18 at 09:04
  • I removed the {id} from the route and now i get Server Error in Application as it tries to browse to http://domain/controller/ – DNKROZ Jan 10 '18 at 09:09
  • Thanks @Thennarasan will check out the link – DNKROZ Jan 10 '18 at 09:10

1 Answers1

1

Don't define your parameters in routes.

ASP.NET automaticaly will add the question mark.

You can then call http://domain/controller/Action?id=17 and it will route to

public ActionResult Action(int id) { }

Update: If you want to kill domain/controller/action/id format completely, you need to define the route as:

routes.MapRoute(
   name: "Parameterless", //or any name
   url: "YourController",
   defaults: new { controller = "YourController", action = "YourAction" }
);

Now you can use domain/controller/action?id={id} and domain/controller/action/id will 404.

If you are getting a Server Application Error, you need to provide more details, since it might be related to something else.

Mithgroth
  • 1,114
  • 2
  • 13
  • 23
  • I removed the {id} from the route and now i get `Server Error in Application` as it tries to browse to `http://domain/controller/` – DNKROZ Jan 10 '18 at 09:09
  • You can keep the default definition. What happens when you default to `{controller}/{action}/{id}` and try to call `domain/controller/Action?id=17` ? – Mithgroth Jan 10 '18 at 09:11
  • Sorry we can ignore the first route as this is for a different controller. I will take it out of my question. When I default to the above and browse to the link it works as expected, however I would like the above query string to be entered into the URL every time, instead of `domain/controller/action/17`. If you get what I mean – DNKROZ Jan 10 '18 at 09:16
  • Just tested it. If you want to kill `domain/controller/action/17` for good, remove the `/{id}` part from `{controller}/{action}/{id}` with `, id = UrlParameter.Optional` section from defaults too, it works as you want. I'll update my answer. – Mithgroth Jan 10 '18 at 09:34
  • It stops me being able to browse to that link, but it also causes a `Server Error in '/' Application` when I then click a button which calls the action. To be honest I'm not even sure it's possible to do what I'm asking – DNKROZ Jan 10 '18 at 09:37
  • It's not as such I want to "use" `domain/controller/action?id={id}` it's that I want that format to always show in the URL as oppose to `domain/controller/action/id`. This is not by me typing the URL myself but by button click which calls an action – DNKROZ Jan 10 '18 at 09:41
  • After quite a bit of research i don't think this is possible – DNKROZ Jan 10 '18 at 09:42
  • Oh, you want your other links to be redirected that way? – Mithgroth Jan 10 '18 at 09:42
  • You can always type your button redirections by hand and avoid using the helper method. If you `` it should redirect you to the url you want. – Mithgroth Jan 10 '18 at 09:52
  • Good point - I could do that, however my solution is huge so looking for something quicker and a more global solution – DNKROZ Jan 10 '18 at 09:55
  • I doubt that would be solved with routing only since `?` is a reserved character. I might only suggest you to `CTRL + SHIFT + H` to do a replace all. – Mithgroth Jan 10 '18 at 09:58