Reputation: 1969
I want to redirect users when they enter a URL into the browser based on the ID. For example, a user enters:
http://localhost:50431/10213
and they will be redirected to:
http://localhost:50431/home/job/10213
Default Route:
routes.MapRoute(
name: "Default",
url: "{controller}/{action}/{jobno}",
defaults: new { controller = "Home", action = "Job", jobno = UrlParameter.Optional }
);
How can I achieve this?
Upvotes: 0
Views: 1161
Reputation:
I think this is the best solution:
routes.MapRoute(
name: "Job Number",
url: "{jobno}",
defaults: new { controller = "Home", action = "Job", .jobno = UrlParameter.Optional },
constraints: new { jobno = @"[0-9]*" }
);
I have tested this and it works properly, his reply in the comment did not work properly for me.
Upvotes: 0
Reputation: 7590
You could try something like this:
routes.MapRoute(
name: "Job Number",
url: "{jobno}",
defaults: new { controller = "Home", action = "Job" },
new { jobno = @"[0-9]*" }
);
And put it above the other route. The added parameter is to avoid the route catching urls like http://localhost:50431/foobar
, but only the ones that contain numbers.
Please note I don't have a way of testing this at the moment so you may have to tweak it slightly.
Upvotes: 1