0

I have two routes set up in my application:

routeCollection.MapRoute("CustomerActivity",
                         "Customer/{id}",
                         new { controller = "CustomerActivity", action = "DisplayDetails" },
                         new { id = @"^\d$" });
routeCollection.MapRoute("CustomerSearch",
                         "Customer/{*pathInfo}",
                         new
                            {
                             controller = "CustomerSearch",
                             action = "DisplaySearch"
                            });

Incoming Urls are correctly routed to the right controller/action pairing. However in the view I need to generate an Anchor to view the details of the customer thus:

@Html.ActionLink(Model.Name, "DisplayDetails", "CustomerActivity", new { id = Model.Id }, null)

The problem with this is it doesn't actually pick any of the routes up; I believe this is because of the constraint on the CustomerActivity route.

I've also tried using the RouteLink without much luck:

@Html.RouteLink(Model.Name, "CustomerActivity", new { id = Model.Id })

I can't take out the constraint on CustomerActivity as this is what stops everything falling into that route.

Adding a copy of CustomerActivity without the constraint to the end seems to resolve the problem, but I'm less than impressed:

routeCollection.MapRoute("CustomerActivity",
                         "Customer/{id}",
                         new { controller = "CustomerActivity", action = "DisplayDetails" },
                         new { id = @"^\d$" });
routeCollection.MapRoute("CustomerSearch",
                         "Customer/{*pathInfo}",
                         new
                            {
                             controller = "CustomerSearch",
                             action = "DisplaySearch"
                            });
routeCollection.MapRoute("CustomerActivityUrlCreation",
                         "Customer/{id}",
                         new { controller = "CustomerActivity", action = "DisplayDetails" });

The only other thing I can think of doing is to radically differentiate the Urls and get rid of the constraint on CustomerActivity, but I'd prefer not to do this. Does anyone else have any other suggestions on how to resolve this?

2 Answers 2

1

This should work. I suspect that your Model.Id is greater than 9 and thus failing your constraint which allows only a single digit. So try adjusting the constraint by allowing multiple digits:

routes.MapRoute(
    "CustomerActivity",
    "Customer/{id}",
    new { controller = "CustomerActivity", action = "DisplayDetails" },
    new { id = @"^\d+$" }
);
Sign up to request clarification or add additional context in comments.

1 Comment

Spot on, fixed the problem! Thanks a lot for that!
0
Try this...It will work

routeCollection.MapRoute("CustomerActivity",
                         "{controller}/{action}/{id}",
                         new { controller = "CustomerActivity", action = "DisplayDetails" },
                         new { id = @"^\d$" });

routeCollection.MapRoute("CustomerSearch",
                         "CustomerSearch/DisplaySearch/{*pathInfo}",
                         new
                            {
                             controller = "CustomerSearch",
                             action = "DisplaySearch"
                            });

2 Comments

There are a couple of problems with this answer: 1) The first route is more generic than the second one so will be used mostly; 2) The first route still has the problem that it expects the id to only be one digit; 3) It doesn't maintain the /Customer/ url I was working with.
First of all you don't have default route in routing... and when you are creating your route then you have to pass the url like - {controller}/{action}/{id} and you are passing only "Customer/{id}".and what do you mean to /Customer/ url. If you see the call thatyou are making to CutomerActivity cotroller with DisplayDetails action. So you have to more speific where are making the call.If it's not clear then please let me know what you want because this is tested from my side

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.