2

Route

  routes.MapRoute(
                 name: "Default",
                 url: "{controller}/{action}/{id}",
                 defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional }
             );
    routes.MapRoute(
                    name: "Contact",
                    url: "Contact",
                    defaults: new { controller = "Home", action = "Contact" }
                );

my controller

 public class HomeController : BaseController
    {
        public ActionResult Contact()
        {
            ViewBag.Message = "Your contact page.";

            return View();
        }
    }

My Global.asax

protected void Application_Start()
        {
            AreaRegistration.RegisterAllAreas();

            WebApiConfig.Register(GlobalConfiguration.Configuration);
            FilterConfig.RegisterGlobalFilters(GlobalFilters.Filters);
            RouteConfig.RegisterRoutes(RouteTable.Routes);
            BundleConfig.RegisterBundles(BundleTable.Bundles);
            AuthConfig.RegisterAuth();
        }

Finally my request url

http://localhost:1234/Contact/

Error on browser

The resource cannot be found.

Description: HTTP 404. The resource you are looking for (or one of its dependencies) could have been removed, had its name changed, or is temporarily unavailable. Please review the following URL and make sure that it is spelled correctly.

Requested URL: /Contact/

Version Information: Microsoft .NET Framework Version:4.0.30319; ASP.NET Version:4.0.30319.18033

What am I doing wrong?

Solution:

Custom route should take the precedence

   routes.MapRoute(
                        name: "Contact",
                        url: "Contact",
                        defaults: new { controller = "Home", action = "Contact" }
                    );
 routes.MapRoute(
                     name: "Default",
                     url: "{controller}/{action}/{id}",
                     defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional }
                 );
HaBo
  • 13,999
  • 36
  • 114
  • 206

2 Answers2

12

The Framework always tries to match the URL of the request to a route in the order of the Routes added to the RouteCollection

So you should put the custom routes before the default route,

 //Custom route
 routes.MapRoute(
                    name: "Contact",
                    url: "Contact",
                    defaults: new { controller = "Home", action = "Contact" }
                );
 //default route
 routes.MapRoute(
                 name: "Default",
                 url: "{controller}/{action}/{id}",
                 defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional }
             );
ssilas777
  • 9,672
  • 4
  • 45
  • 68
  • but then debugger stucks in the custom route and doesn't go to the default route on startup as well – Zaveed Abbasi Apr 22 '14 at 10:21
  • @ZaveedAbbasi I don't think this can cause any issue, if you find any issues its better to raise an question with code samples. – ssilas777 Apr 22 '14 at 11:28
  • actually i had at..... "http://stackoverflow.com/questions/23217271/mvc-routing-with-multiple-parameters-in-not-working/23217364?noredirect=1#comment35517982_23217364" – Zaveed Abbasi Apr 22 '14 at 11:33
  • this solved my problem "http://stackoverflow.com/questions/14169796/route-with-two-optional-parameters-in-mvc3-not-working" – Zaveed Abbasi Apr 22 '14 at 12:47
0
You can use:

routes.MapRoute(
            name: "Default",
            url: "{*p}",
            defaults: new { controller = "Home", action = "Index", p = UrlParameter.Optional }
        );
The asterisk indicates that it's a catch-all route. Keep in mind that these routes are extremely greedy, make sure that this stays below any specific routes.

You could also add a route constraint to this route which can determine whether the page exists in the database or something. 
Shiva Saurabh
  • 1,281
  • 2
  • 25
  • 47