idontknowhow
idontknowhow

Reputation: 1527

How can structure mostly static pages in ASP.NET MVC

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?

Upvotes: 3

Views: 251

Answers (3)

JonoW
JonoW

Reputation: 14229

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
}

Upvotes: 3

Kev
Kev

Reputation: 119806

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.

Upvotes: 0

Adrian Godong
Adrian Godong

Reputation: 8911

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();
    }
}

Upvotes: 5

Related Questions