2

In my home controller, I have 3 Action methods. which is give below.

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


        public ActionResult Index2(int did,int docType)
        {
            return View();
        }


        public ActionResult Index3(int uid,int docId,int typeId)
        {
            return View();
        }

As I had given diffrent parameter name in each action methods so I have to change in Route.config.

I have Done this

Method 1

routes.MapRoute(
                name: "Home",
                url: "{controller}/{action}/{did}/{docType}",
                defaults: new { controller = "Home", action = "Index2", did = UrlParameter.Optional, docType = UrlParameter.Optional }
            );
            routes.MapRoute(
                name: "Home",
                url: "{controller}/{action}/{uid}/{docId}/{typeId}",
                defaults: new { controller = "Home", action = "Index3", uid = UrlParameter.Optional, docId = UrlParameter.Optional, typeId = UrlParameter.Optional }
            );
            routes.MapRoute(
                name: "Default",
                url: "{controller}/{action}/{id}",
                defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional }
            );

but it is giving me exception like

Home' is already in the route collection. Route names must be unique

So I have change it to like this

Method 2

 routes.MapRoute(
            name: "Default",
            url: "{controller}/{action}/{id}/{did}/{docType}/{uid}/{docId}/{typeId}",
            defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional, did = UrlParameter.Optional, docType = UrlParameter.Optional, uid = UrlParameter.Optional, docId = UrlParameter.Optional, typeId = UrlParameter.Optional }
        );

When I hit Url like

http://localhost:50958/Home/Index/2

http://localhost:50958/Home/Index2/2/3

http://localhost:50958/Home/Index3/2/3/4

it throwing me exception.

This is Solution.

As suggested by Stephen Muecke

 routes.MapRoute(
                name: "Admin",
                url: "{controller}/{action}/{did}/{docType}",
                defaults: new { controller = "Home", action = "Index2", did = UrlParameter.Optional, docType = UrlParameter.Optional }
            );
            routes.MapRoute(
                name: "User",
                url: "{controller}/{action}/{uid}/{docId}/{typeId}",
                defaults: new { controller = "Home", action = "Index3", uid = UrlParameter.Optional, docId = UrlParameter.Optional, typeId = UrlParameter.Optional }
            );
            routes.MapRoute(
                name: "Default",
                url: "{controller}/{action}/{id}",
                defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional }
            );
6
  • 1
    The reason for the first error is because your first 2 route definitions have name: "Home", - they need to be unique. Just change one or the other Commented May 21, 2015 at 10:56
  • @StephenMuecke:But my all the actions are in Home Controller. Commented May 21, 2015 at 10:57
  • That has nothing at all do do with it. Call then anything you want. They just need to be unique Commented May 21, 2015 at 10:58
  • @StephenMuecke: thanks . It works. can it be possible by method 2. Commented May 21, 2015 at 10:59
  • I doubt it. Only the last parameter can be optional so you would need to provide at least 5 parameters every time to avoid ambiguity. Note also the name property is used by methods such as @Html.RouteLink("XYZ"); which matches routes.MapRoute( name: "XYZ, ...) Commented May 21, 2015 at 11:04

2 Answers 2

1

Route name should be unique, You have two routes with same name: "Home"

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

Comments

1

As I had given same name to all route. and route name must be unique, now i have rename the route with different name.

routes.MapRoute(
                name: "Admin",
                url: "{controller}/{action}/{did}/{docType}",
                defaults: new { controller = "Home", action = "Index2", did = UrlParameter.Optional, docType = UrlParameter.Optional }
            );
            routes.MapRoute(
                name: "User",
                url: "{controller}/{action}/{uid}/{docId}/{typeId}",
                defaults: new { controller = "Home", action = "Index3", uid = UrlParameter.Optional, docId = UrlParameter.Optional, typeId = UrlParameter.Optional }
            );
            routes.MapRoute(
                name: "Default",
                url: "{controller}/{action}/{id}",
                defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional }
            );

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.