Reputation: 7126
Might not be a very consequential question, but...
I have a bunch of mostly-static pages: Contact, About, Terms of Use, and about 7-8 more.
Should each of these have their own controllers, or should I just have one action for each?
Thanks in advance.
Upvotes: 4
Views: 2233
Reputation: 21502
The best way to add static page to your mvc app is to create a separate controller called Pages
(or whatever you want ) and passing the page name to their Index
method as a parameter.
So you need to test if the page exist before render it, if it exist render it , else render your custom Page Not Found
page.
here is an example :
In 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);
}
}
Upvotes: 7
Reputation: 2423
The recommended use case in ASP.NET MVC is to create a separate controller per model and separate action methods on the controller per concern, i.e. action each for edit, create, retrieve and delete for example.
However, in this use case since you mentioned that all you have are static pages it seems the job can be done via the use of one controller and separate action methods corresponding to each views. It can be implemented as so,
public HomeController : Controller
{
public ActionResult About()
{
return View();
}
public ActionResult TermsOfUse()
{
return View();
}
public ActionResult Contact()
{
return View();
}
// etc ........
}
HomeController
is chosen here since it comes by default in ASP.NET MVC projects and is wired to the default route in Global.asax.cs
file.
Upvotes: 3
Reputation: 47766
Just consider each item individually. If you think that there is even the slightest chance that the action/view could be required to be more than just static data then break it up now or else you will have to worry about breaking links in the future that your customers/visitors/search engines have saved/indexed and now you will need to possibly maintain some redirects etc.
If you are sure that it will never change (ha who says never) then use one controller with many actions/views. Eg:
http://yoursite.com/home/contact
http://yoursite.com/home/terms
http://yoursite.com/home/about
etc...
This will save your project from a lot of clutter for very basic data assuming that you will have a lot of other code. If the majority of you code is just these pages though you should break it up because there is nothing to clutter up anyways.
Upvotes: 2
Reputation: 2049
There is no harm in having one controller with an action for each page. I would likely make a HomeController with an ActionResult and corresponding aspx view for Contact, About, Terms of Use, etc... For this simple case, if you want more control over the url or there is a logical grouping of the controllers that makes sense then you may want to break it into 2 or more controllers.
Upvotes: 1