Pergunta

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 }
                 );
Foi útil?

Solução

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 }
             );

Outras dicas

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. 
Licenciado em: CC-BY-SA com atribuição
Não afiliado a StackOverflow
scroll top