Darren
Darren

Reputation: 11011

Area routes being ignored?

I have these routes:

routes.MapRoute(
    "Advertisers",
    "advertisers/{controller}/{action}/{id}",
    new { controller = "Index", action = "Index", id = UrlParameter.Optional },
    new string[] { "Portal.Areas.Advertisers.Controllers" }
);

routes.MapRoute(
    "Root", // Route name
    "{controller}/{action}/{id}", // URL with parameters
    new { controller = "Index", action = "Index", id = UrlParameter.Optional }, // Parameter defaultsm
    new string[] { "Portal.Controllers" }
);

However whenever I go to /advertisers/controller/action/id it is not reading the id parameter... what am I doing wrong?

Thanks

Upvotes: 0

Views: 50

Answers (1)

Chase Florell
Chase Florell

Reputation: 47387

I'd suggest you take a look at the Route Debugger

nuget install

PM> Install-Package routedebugger

After you've installed it into your project, put this one line of code inside your application start method, and hit the url you're debugging.

protected void Application_Start()
{
    RouteDebug.RouteDebugger.RewriteRoutesForTesting(RouteTable.Routes);
}

This will tell you exactly why your routes aren't working as expected.

As for your actual question, is your Controller actually called IndexController? Because this doesn't seem right to me

public class IndexController : Controller
{

    public ActionResult Index()
    {
        return View();
    }

}

My assumption is that you actually have something like HomeController or AdvertiserController, and if that's the case you should have something like this

routes.MapRoute(
    "advertisers_default", // Route name
    "advertisers/{controller}/{action}/{id}/{advertiserName}", // URL with parameters
    new { controller = "Home", 
          action = "Index", 
          advertiserName = UrlParameter.Optional },
    new { id = "[0-9]+",
          controller = "[a-zA-Z]+",
          action = "[a-zA-Z]+" }
);

and then hit the url http://example.com/advertisers/{id}/{advertiser-name}

Simply said, this url looks wrong to me

/advertisers/controller/action/{id}

it should be
/advertisers/home/{id}

or even
/advertisers/home/{id}/{advertiser-name}

Upvotes: 1

Related Questions