Structuring multiple mostly static pages in ASP.NET MVC

It may not be a very indirect question, but ...

I have a bunch of mostly static pages: Contacts, About Us, Terms of Use, and another 7-8.

If each of them has its own controllers or do I have only one action for each?

Thanks in advance.

+4
source share
4 answers

Just review each item separately. If you think that there is at least the slightest chance that an action / presentation may be required not just by static data, then break it now, otherwise you will have to worry about future breaking links that your customers / visitors / searches engines saved / indexed, and now you will need to support some redirects, etc.

If you are sure that it will never change (ha, which never says), use one controller with many actions / views. For instance:

http://yoursite.com/home/contact http://yoursite.com/home/terms http://yoursite.com/home/about etc... 

This will save your project due to the large amount of interference for obtaining very simple data, assuming that you will have a lot of other code. If most of you code only these pages, although you should break it down, because there is nothing to clutter anything.

+2
source

The best way to add a static page to your mvc application is to create a separate controller named Pages (or whatever) and pass the page name to your Index method as a parameter. Therefore, you need to check if the page exists before rendering, if it exists, and also display your custom Page Not Found page. here is an example:

At Global.asax:

 // put the StaticPage Rout Above the Default One routes.MapRoute( "StaticPages", "Pages/{page}", new { controller = "Pages", action = "Index"} ); routes.MapRoute( "Default", "{controller}/{action}/{id}", new { controller = "Home", action = "Index", id = UrlParameter.Optional} ); 

Create a controller called PagesController:

 public class PagesController : Controller { public ActionResult Index(string page) { // if no paramere was passed call the default page if (string.IsNullOrEmpty(page)) { page = "MyDefaultView"; } // Check if the view exist, if not render the NotfoundView if (!ViewExists(page)) { page = "PageNotFoundView"; } return View(page); } // function that check if view exist private bool ViewExists(string name) { ViewEngineResult result = ViewEngines.Engines.FindView(ControllerContext, name, null); return (result.View != null); } } 
+7
source

The recommended use case in ASP.NET MVC is to create a separate controller for each model and separate action methods for the controller , i.e. action for editing, creating, extracting and deleting, for example.

However, in this use case, since you mentioned that all you have is static pages, it seems that the work can be done using one controller and separate action methods corresponding to each kind. It can be implemented in such a way

 public HomeController : Controller { public ActionResult About() { return View(); } public ActionResult TermsOfUse() { return View(); } public ActionResult Contact() { return View(); } // etc ........ } 

HomeController is selected here because it comes by default in ASP.NET MVC projects and is connected to the default route in the Global.asax.cs file.

+3
source

There is no harm in the fact that one controller has an action for each page. I would most likely make a HomeController with an ActionResult and an appropriate aspx representation for Contact, About, Terms of Use, etc. In this simple case, if you want more control over the URL or there is a logical grouping of controllers that does, then you can split it into 2 or more controllers.

+1
source

All Articles