ftdeveloper
ftdeveloper

Reputation: 1093

Asp.net Mvc Routing issue

I have controller named PartialController and have a action method named AboutMe. I can access my action method from localhost/Partial/AboutMe . But i want to access it from localhost/About. So i changed my route like this:

 routes.IgnoreRoute("{resource}.axd/{*pathInfo}");

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

        routes.MapRoute(
        name: "About",
        url: "About",
        defaults: new
            {
                controller = "Partial",
                action = "AboutMe",
                id = UrlParameter.Optional
            }
        );

But i got 404 exception when trying to access AboutMe action from localhost/About. Need advice.

Upvotes: 0

Views: 43

Answers (1)

WannaCSharp
WannaCSharp

Reputation: 1898

Define your About Route first. Order matters when it comes to routing, they are applied in the order in which they are defined.

  routes.MapRoute(
        name: "About",
        url: "About",
        defaults: new
            {
                controller = "Partial",
                action = "AboutMe",
                id = UrlParameter.Optional
            }
        );

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

Upvotes: 1

Related Questions