0

I m trying to achive route like that:

http://mysite.com/portfolio/landscape

http://mysite.com/portfolio/friends etc...

so I wrote that:

public static void RegisterRoutes(RouteCollection routes)
        {
            routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
            routes.MapRoute(
                "DefaultIndex", // Route name
                "{controller}/{id}", // URL with parameters
                new { action = "Index", id = UrlParameter.Optional } // Parameter defaults
            );
            routes.MapRoute(
                "Default", // Route name
                "{controller}/{action}/{id}", // URL with parameters
                new { controller = "Home", action = "Index", id = UrlParameter.Optional } // Parameter defaults
            );

        }

It works well I can have my route /portfolio/landscape but my Account controler that have SignIn, SignOut, Index actions doesn't work because it gets redirected to Index each time.

is it possible to get both?

Thank you by advance

3 Answers 3

5

Try to introduce a constraint in your custom route otherwise it won't allow default route to be found.

        routes.MapRoute(
            "DefaultIndex", // Route name
            "portfolio/{id}", // URL with parameters
            new { controller="portfolio", action = "Index", id = UrlParameter.Optional }
        );

This way you only map URLs starting with "portfolio" in your route, and specify which controller and action. Requests for other URLs are handled by the default route.

Sign up to request clarification or add additional context in comments.

Comments

0

I think you can just reverse the order of route declaration.

public static void RegisterRoutes(RouteCollection routes)
    {
        routes.MapRoute(
            "Default", // Route name
            "{controller}/{action}/{id}", // URL with parameters
            new { controller = "Home", action = "Index", id = UrlParameter.Optional }     // Parameter defaults
        );
        routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
        routes.MapRoute(
            "DefaultIndex", // Route name
            "{controller}/{id}", // URL with parameters
            new { action = "Index", id = UrlParameter.Optional } // Parameter defaults
        );


    }

If you mention any Controller and action it will go to that else it will pick the default

Comments

0

Assuming there is a good reason for the existing routes, here is one way to make AccountController play nice with those:

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

        routes.MapRoute(
            "Account", // Account name
            "account/{action}/{id}", // URL with parameters
            new { controller = "Account", action = "Index", id = UrlParameter.Optional } // Parameter defaults
        );

        routes.MapRoute(
            "DefaultIndex", // Route name
            "{controller}/{id}", // URL with parameters
            new { action = "Index", id = UrlParameter.Optional } // Parameter defaults
        );
        routes.MapRoute(
            "Default", // Route name
            "{controller}/{action}/{id}", // URL with parameters
            new { controller = "Home", action = "Index", id = UrlParameter.Optional } // Parameter defaults
        );

    }

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.