views:

86

answers:

2

I have the following two routes defined in my MVC app.;- At the moment I have two "MVC View content pages" defined

/ShowName/NameById
/ShowName/Index

However the content on these two pages are identical? Is it possible for two routes to share the same content page? If not then can I a) create a single rule for both routes or b) should I create a usercontrol to share between both content pages to show my data?

    routes.MapRoute(
       "NameById",
       "Name/{theName}/{nameId}",
        new
        {
            action = "NameById",
            controller = "ShowName",
            theName = "Charley"
        }
        ,new { nameId = @"\d+" }
   );

    routes.MapRoute(
       "ShowName",
       "Name/{theName}",
        new
        {
            action = "Index",
            controller = "ShowName",
            theName = "Charley"
        }
   );

EDIT I have read the answers below and I have the following action result methods. If I remove one of the methods (e.g. the Index) then how would I rewrite my routes to a single route?

public ActionResult Index(string theName)
public ActionResult NameById(string theName, int? nameId)

So the following works url's work?

/Name/Charley
/Name/Charley/11234
A: 

Do you really need 2 different routes? You could make the pattern for your Index route

Name/{theName}/{nameId}

and make nameId a nullable input to your action. Then just add some logic to your action which checks whether nameId has a value and acts accordingly.

pmarflee
+1  A: 

You could create a partial view for the detail area of the page, keeping the separation of the two actions in case they change at a later point. Or you could just

return View("DetailView", model);

But that can introduce an extra string to manage between the two controller actions. Since MVC does not support overloading by action name (unless you have a GET/POST pair, one with no arguments), you could just check the {nameId} parameter and see if it is empty/null before using it.

Chris Patterson