title: MVC mapping routes to navigation menu and avoiding hard-coding links
body: <p>I have setup my routes so that I have the following flat URL structure:</p>
  
  <pre><code>mywebsite/service-option-one (goes to Home Controller, Option1 action)
  mywebsite/service-option-two (goes to Home Controller, Option2 action)
  mywebsite/ (goes to Home Controller, Index)
  mywebsite/about (goes to Home Controller, Index with path=about)
  mywebsite/contact (goes to Home Controller, Index with path=contact)
  </code></pre>
  
  <p>This is important as I have a lot of content views and do not want to have individual actions for these generic information pages, the simple code for the resolving the view is at the end of this post.</p>
  
  <p>When building the menu the MVC <code>Html.ActionLink</code> helpers, but they give the incorrect addresses for generic content which makes sense as these actions do not exist!</p>
  
  <p>Given my address scheme, how can is there a helper method I can use to set my anchor link targets or do i just have to resort to hard coding in html (i.e. <code>&lt;a href="~/contact&gt;Contact us&lt;/a&gt;</code>)?</p>
  
  <pre><code>// note that the order of the routes is very important!
  public static void RegisterRoutes(RouteCollection routes)
  {
      routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
  
      // error route
      routes.MapRoute(
          "Error", // Route name
          "Oops", // URL with parameters
          new { controller = "Home", action = "Error" }
      );
  
      routes.MapRoute(
          "Service1",
          "service-option-one",
          new { controller = "Home", action = "Option1" }
      );
  
      routes.MapRoute(
          "Service2",
          "service-option-two",
          new { controller = "Home", action = "Option1" }
      );
  
      // default home controller route for general site content (/content), uses default path value set to Index
      routes.MapRoute(
          name: "Catchall",
          url: "{path}",
          defaults: new { controller = "Home", action = "Index", path = "Index" }
      );
  
      // home page route, blank url (/)
      routes.MapRoute(
          "HomePage", // Route name
          "", // URL with parameters
          new { controller = "Home", action = "Index" }
      );
  
      // default route
      routes.MapRoute(
          "Default", // Route name
          "{controller}/{action}/{id}", // URL with parameters
          new { controller = "{controller}", action = "{action}", id = UrlParameter.Optional } // Parameter defaults
      );
  }
  
  
  public class HomeController : Controller
  {
      private bool ViewExists(string name)
      {
          ViewEngineResult result = ViewEngines.Engines.FindView(ControllerContext, name, null);
          return (result.View != null);
      }
  
      public ActionResult Index(string path)
      {
          System.Diagnostics.Debug.WriteLine("looking for..." + path);
  
          if (ViewExists(path)==true)
          {
              System.Diagnostics.Debug.WriteLine("general controller action...");
              return View(path);
          }
          else
          {
              System.Diagnostics.Debug.WriteLine("page not found...");
              return RedirectToRoute("Error");
          }
      }
  
      public ActionResult Error()
      {
          return View();
      }
  
      public ActionResult Option1()
      {
          return View();
      }
  
      public ActionResult Option2()
      {
          return View();
      }
  }
  </code></pre>