C Sharper
C Sharper

Reputation: 8626

Custom Route Not Matched

In routeConfig i have code:

public static void RegisterRoutes(RouteCollection routes)
        {
            routes.IgnoreRoute("{resource}.axd/{*pathInfo}");

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

            routes.MapRoute(
                name: "Custom",
                url: "Secret/Routes/1",
                defaults: new { controller = "Account", action = "Login", id = UrlParameter.Optional }
            );
        }

I have created Custom Route as:

 routes.MapRoute(
                    name: "Custom",
                    url: "Secret/Routes/1",
                    defaults: new { controller = "Account", action = "Login", id = UrlParameter.Optional }
                );

But when i am giving direct link as:

http://localhost:4618/Secret/Routes/1

Its not rendering to Account Controller's Login Action.

Please help me. Where i am making mistake in route???

Want to render to Account Controller Login Action when URL is

http://localhost:4618/Secret/Routes/1

Upvotes: 0

Views: 54

Answers (1)

Mohsen Esmailpour
Mohsen Esmailpour

Reputation: 11544

When routing handles URL requests, it tries to match the URL of the request to a route. Matching a URL request to a route depends on all the following conditions:

  • The route patterns that you have defined or the default route patterns, if any, that are included in your project type.

  • The order in which you added them to the Routes collection.

  • Any default values that you have provided for a route.

  • Any constraints that you have provided for a route.

  • Whether you have defined routing to handle requests that match a physical file.

More info about ASP.NET Routing.

You have to change order of routes.

    public static void RegisterRoutes(RouteCollection routes)
    {
        routes.IgnoreRoute("{resource}.axd/{*pathInfo}");

        routes.MapRoute(
            name: "Custom",
            url: "Secret/Routes/1",
            defaults: new { controller = "Account", action = "Login", id = UrlParameter.Optional}
        );

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

Upvotes: 1

Related Questions