I have routes like these:
routes.MapPageRoute("Survey", "Survey", "~/Survey/Survey.aspx")
routes.MapPageRoute("Letters", "About/Letters", "~/Pages/Letters/Letters.aspx")
How can I redirect a url like this: /Surveys to the 'Survey' route? So that when the user goes to /surveys it redirects to /Survey. (URLs for the sake of argument)
I'd prefer it if I didn't have to place redirect code in the ASPX file itself, and rather just have the code in the route rule, just keeps it simple and centralized.
Thanks
Luke
Routing in ASP.NET MVC cs file in App_Start Folder, You can define Routes in that file, By default route is: Home controller - Index Method.
ASP.NET routing is used to dispatch a request to a handler based on the requested URL path. As opposed to URL rewriting, the routing module knows about the handlers and selects the handler that should generate a response for the requested URL. You can think of ASP.NET routing as an advanced handler-mapping mechanism.
RedirectToRoute(String, RouteValueDictionary)Redirects to the specified route using the route name and route dictionary.
MapControllerRoute(name: "default", pattern: "{controller=Home}/{action=Index}/{id?}"); The route names give the route a logical name. The named route can be used for URL generation. Using a named route simplifies URL creation when the ordering of routes could make URL generation complicated.
You can use something like this
Response.RedirectToRoute("Survey");
the parameter "Survey" is routeName you defined in Global.asax using MapPageRoute. RedirectToRoute also has other overloads that allow you to pass route parameters if required
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With