Register Default Route

Hello guys,

I am using asp.net C#/3.5 and stuck.


 Route myroute1 = new Route("{controller}/{action}/{slug}", new MyRouteHandler());
            Route myroute2 = new Route("{controller}/{action}", new MyRouteHandler());
            Route myroute3 = new Route("{controller}", new MyRouteHandler());

            Routes.Add(myroute1);
            Routes.Add(myroute2);
            Routes.Add(myroute3);

The problem is:

http://www.mysite.com/account/admin/add - fine
http://www.mysite.com/account/admin/ - 404 error currenctly displayed because there’s no such file.

http://www.mysite.com/account/admin/ - I wish to redirect to default route i.e.
http://www.mysite.com/account/admin/mange - default slug for such condition.

In myrouteHandler:


 urlpath = "~/webforms/" + urlpath;
string fullpath = HttpContext.Current.Server.MapPath(urlpath);
            if (File.Exists(fullpath) == false)
            {
                urlpath = "~/webforms/custom404.aspx";
            }

Please help.

Thanks in advance.

Problem: How to register default routes?


new Route("{controller}/{action}/{slug}",
new RouteValueDictionary { { "controller", "account" }, { "action", "admin" }, {"slug", "manage"} }, // Defaults
new MyRouteHandler());


Route myroute1 = new Route("{controller}/{action}/{slug}", new MyRouteHandler());
myroute1.Defaults = new RouteValueDictionary { { "slug", "form" } }; // working great.

Route myroute2 = new Route("{controller}/{action}", new MyRouteHandler()); 
// its not functional now :(

what’s the problem?

Route myroute2 = new Route(“{controller}/{action}”, new MyRouteHandler());
its redirecting to custom404 page…

routes.MapRoute(

"Default",                                              

"{controller}/{action}/{slug}",                           

new { controller = "", action = "", slug = "manage" }  

);

I m using asp.net web forms and routes.MapRoute() is not available there :slight_smile:

Please suggest.

Oh, I see. My apologies.

Just add to seperate routed to handle it. Just take note of the order in which the routes are in, as that is the most important

tried it already no luck

Can you post your entire route handler code. Something must be going wrong in there. As I usually use different methods for each, although it is not needed. I just want to see your you build your path


urlpath = "~/webforms/" + urlpath; string fullpath = HttpContext.Current.Server.MapPath(urlpath);             if (File.Exists(fullpath) == false)             {                 urlpath = "~/webforms/custom404.aspx";             } 

please check