5

I would like my URLs to use the convention:

/{controller}/{id}/{action}

rather than

/{controller}/{action}/{id}

I tried setting up a route as follows:

routes.MapRoute(
            "Campaign",
            "{controller}/{action}/{id}",
            new { controller = "Campaign", action = "Index", id = UrlParameter.Optional } 
        );

But this doesn't work because I am unable to make the id parameter optional.

The following URLs do work:

/campaign/1234/dashboard
/campaign/1234/edit
/campaign/1234/delete

But these URLs do not:

/campaign/create
/campaign/indexempty

MVC just calls Index for both. What am I doing wrong?

1 Answer 1

7

I think you probably need two separate routes for this.

routes.MapRoute(
            "CampaignDetail",
            "{controller}/{id}/{action}",
            new { controller = "Campaign", action = "Index" } 
        );

routes.MapRoute(
            "Campaign",
            "{controller}/{action}",
            new { controller = "Campaign", action = "Index" } 
        );
Sign up to request clarification or add additional context in comments.

3 Comments

That worked! Slight modification to your code--I named the first route "CampaignDetail" so that it wouldn't conflict with the second route.
Oh, I missed that. I'm glad it helped.
One more thing--Having {controller}/{id}/{action} interfered with my default route. I was able to fix this by explicitly using the word campaign in my url. i.e. campaign/{id}/{action}.

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.