2013-09-29 38 views
0

我安装我的路由,使我有以下的平板URL结构:MVC映射路线导航菜单,避免硬编码链路

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) 

这是重要的,因为我有很多的内容的意见和做不希望对这些通用信息页面进行单独的操作,解析视图的简单代码在本文后面。

建立菜单时,MVC Html.ActionLink助手,但他们给不正确的通用内容的地址,因为这些操作不存在!

鉴于我的地址方案,如何可以使用辅助方法来设置我的锚链接目标,或者我只需要在html中使用硬编码(即<a href="~/contact>Contact us</a>)?

// 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(); 
    } 
} 

回答

1

您正在接近从使用助手时应该使用的相反位置使用ActionLink助手。你试图告诉Razor该链接的路由网址应该是什么,实际上这就是Razor将为你做的事情。因此,对于mywebsite/service-option-one的例子,您应该拥有service-option-one的唯一地方记录在您的路线图中。在你的意见,你应该做

@Html.ActionLink("The Anchor Text You Want To Display","Option1","Home") 

然后,当剃刀呈现视图,它会转化为

<a href="mywebsite/service-option-one">The Anchor Text You Want To Display</a> 

http://msdn.microsoft.com/en-us/library/dd505070(v=vs.118).aspx