Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8134121
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 6, 20262026-06-06T09:53:55+00:00 2026-06-06T09:53:55+00:00

I have a ListItem class that is used to represent menu items in my

  • 0

I have a ListItem class that is used to represent menu items in my application:

 public class ListItem : Entity
{
    public virtual List List { get; set; }
    public virtual ListItem ParentItem { get; set; }
    public virtual ICollection<ListItem> ChildItems { get; set; }

    public int SortOrder { get; set; }
    public string Text { get; set; }
    public string Controller { get; set; }
    public string Action { get; set; }
    public string Area { get; set; }
    public string Url { get; set; }
}

I use this data to construct the routes for the application, but I was wondering if there was a clean way to handle controller/views for static content? Basically any page that doesn’t use any data but just views. Right now I have one controller called StaticContentController, which contains a unique action for each static page that returns the appropriate view like so:

public class StaticContentController : Controller
{

    public ActionResult Books()
    {
        return View("~/Views/Books/Index.cshtml");
    }

    public ActionResult BookCategories()
    {
        return View("~/Views/Books/Categories.cshtml");
    }

    public ActionResult BookCategoriesSearch()
    {
        return View("~/Views/Books/Categories/Search.cshtml");
    }
}

Is there some way I could minimize this so I don’t have to have so many controllers/actions for static content? It seems like when creating my ListItem data I could set the Controller to a specific controller that handles static content, like I have done, but is there anyway to use one function to calculate what View to return? It seems like I still need separate actions otherwise I won’t know what page the user was trying to get to.

The ListItem.Url contains the full URL path from the application root used in creating the route. The location of the View in the project would correspond to the URL location to keep the organization structure parallel.

Any suggestions? Thanks.

Edit: My Route registration looks like so:

public static void RegisterRoutes(RouteCollection routes)
    {
        routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
        routes.IgnoreRoute("Shared/{*pathInfo}");
        routes.MapRoute("Access Denied", "AccessDenied", new { controller = "Shared", action = "AccessDenied", area = "" });
        List<ListItem> listItems = EntityServiceFactory.GetService<ListItemService>().GetAllListItmes();
        foreach (ListItem item in listItems.Where(item => item.Text != null && item.Url != null && item.Controller != null).OrderBy(x => x.Url))
        {
            RouteTable.Routes.MapRoute(item.Text + listItems.FindIndex(x => x == item), item.Url.StartsWith("/") ? item.Url.Remove(0, 1) : item.Url, new { controller = item.Controller, action = item.Action ?? "index" });
        }

        routes.MapRoute(
            "Default", // Route name
            "{controller}/{action}/{id}", // URL with parameters
            new { controller = "Home", action = "Index", id = UrlParameter.Optional } // Parameter defaults
        );


    }
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-06T09:53:58+00:00Added an answer on June 6, 2026 at 9:53 am

    You can use a single Action with one parameter (the View name) which will return all the static pages

    public class StaticContentController : Controller
    {
        public ActionResult Page(string viewName)
        {
            return View(viewName);
        }
    }
    

    You will also need to create a custom route for serving these views, for example:

    routes.MapRoute(
        "StaticContent",                                      // Route name
        "page/{viewName}",                                    // URL with parameters
        new { controller = "StaticContent", action = "Page" } // Parameter defaults
    );
    

    I see in your example that you specify different folders for your views. This solution will force you to put all static views in the Views folder of the StaticContentController.

    If you must have custom folder structure, then you can change the route to accept / by adding * to the {viewName} like this {*viewname}. Now you can use this route: /page/Books/Categories. In the viewName input parameter you will receive "Books/Categories" which you can then return it as you like: return View(string.Format("~/Views/{0}.cshtml", viewName));

    UPDATE (Avoiding the page/ prefix)

    The idea is to have a custom constraint to check whether or not a file exists. Every file that exists for a given URL will be treated as static page.

    public class StaticPageConstraint : IRouteConstraint
    {
        public bool Match(HttpContextBase httpContext, Route route, string parameterName, RouteValueDictionary values, RouteDirection routeDirection)
        {
            string viewPath = httpContext.Server.MapPath(string.Format("~/Views/{0}.cshtml", values[parameterName]));
    
            return File.Exists(viewPath);
        }
    }
    

    Update the route:

    routes.MapRoute(
        "StaticContent",                                       // Route name
        "{*viewName}",                                         // URL with parameters
        new { controller = "StaticContent", action = "Page" }, // Parameter defaults
        new { viewName = new StaticPageConstraint() }          // Custom route constraint
    );
    

    Update the action:

    public ActionResult Page(string viewName)
    {
        return View(string.Format("~/Views/{0}.cshtml", viewName));
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a ul list, with 10 items, and I have a label that
I have a list of the following object: public class userReview { public long
I have a ListItem (Sharepoint), but lets think of it as a simple class/entity.
I have a Backbone collection jQuery -> class App.Collections.List extends Backbone.Collection model: App.Models.ListItem I
I have the following code: foreach (ListItem item in check.Items) { if (item.Selected) {
I have some code that inserts a list item into a list... I then
I have an unordered list which contains several items called 'oListItems' the UL has
I have a script that checks the class (integer) of a , runs a
I understand that there's a set of methods used to pass data from one
I have used a custom array adapter to populate my list view.The problem i

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.