How to 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 build this in my controller? This is what I previously encoded in my game controller:

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

But this gives me an error due to a hyphen in the name of the controller action.

How to resolve this?

+3
source share
3 answers

You may need some sort of catch-all route:

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

You can redirect this route to the controller method:

public class GamesController : Controller
{
    public ActionResult ViewGame(string platform, string game)
    {
        // do whatever
        return View();
    }
}
+5
source

, - , ActionName , , , :

[ActionName("ben-10")]
public ActionResult ben10()
{    
   return View(); //view is assumed to be ben-10.aspx, not ben10.aspx
}
+3

Definitions are not allowed in method and class names. However, emphasized _. What you can do is rewrite the names of portable pages and replace the hyphen with an underscore.

However, Adrianโ€™s answer makes more sense, otherwise you will create and manage more controllers and actions than is really necessary.

0
source

Source: https://habr.com/ru/post/1768190/


All Articles