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?

3 Answers 3

5

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();
    }
}
Sign up to request clarification or add additional context in comments.

Comments

3

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
}

Comments

0

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.

2 Comments

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 yoursite.com/games/ben-10 what i I like to have is yoursite.com/games/psp/ben-10.aspx .thanks you so much.. .
@idontknowhow - To be honest I'd go with Adrians suggestion otherwise you'll be creating and managing more controllers and actions than is necessary.

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.