tags:

views:

51

answers:

3

I have mostly static view pages, for example:

http://www.yoursite.com/games/x-box-360/nba-2k-11.aspx http://www.yoursite.com/games/psp/ben-10.aspx

How can I construct this in my controller? This is what I coded earlier in my games controller:

[HandleError]
public class GamesController : Controller
{
    public ActionResult ben-10()
    {    
        return View();
    }
}

But it gives me an error because of the hyphen in the controller action name.

How do I resolve this?

A: 

Hyphens aren't permitted in method and class names. However underscores _ are. What you could do is rewrite the hyphenated page names and replace the hyphen with an underscore.

That said, Adrians answer makes far more sense otherwise you'll be creating and managing more controllers and actions than is actually necessary.

Kev
thanks for the respond.. .but how about the games subfolder like psp and xbox how can it be done with url route? or in other easy way too..the above will give http://www.yoursite.com/games/ben-10/ what i I like to have is http://www.yoursite.com/games/psp/ben-10.aspx .thanks you so much.. .
idontknowhow
@idontknowhow - To be honest I'd go with Adrians suggestion otherwise you'll be creating and managing more controllers and actions than is necessary.
Kev
+4  A: 

What you probably need is some sort of "catch all" route:

"/games/{platform}/{game}"

You can redirect this route to a controller method:

public class GamesController : Controller
{
    public ActionResult ViewGame(string platform, string game)
    {
        // do whatever
        return View();
    }
}
Adrian Godong
thanks you so much Adrian it works now pretty.. .
idontknowhow
+2  A: 

Adrians answer is correct, but to get around the hyphen issue and still use the default route, you can add an ActionName attribute to your action method to override the name it routes against, e.g.:

[ActionName("ben-10")]
public ActionResult ben10()
{    
   return View(); //view is assumed to be ben-10.aspx, not ben10.aspx
}
JonoW
thank you so much too JonoW.. .It will not resolve this guys without your help.. .thank you thank you.. .
idontknowhow