2013-08-22 120 views
0

我想繞過一些自定義路由,但我得到404s時,當我嘗試訪問我的網站。自定義路由.net MVC 4

我在我的Global.asax嘗試這兩種

public static void RegisterRoutes(RouteCollection routes) 
{ 
    routes.MapRoute(
     name: "Show", 
     url: "{controller}/{action}/{trip}/{year}/{user}", 
     defaults :new { controller = "Home", action = "Show" } 
     ); 
} 

public static void RegisterRoutes(RouteCollection routes) 
{ 
    routes.MapRoute(
     "Default", 
     "{controller}/{action}/{trip}/{year}/{user}", 
     new { controller = "Home", action = "Show", trip = "", year = "", user = "" } 
     ); 
} 

這裏是控制器的操作方法:

public ActionResult Show(string trip, string year, string user) 
{ 
    ViewBag.imagepath = "/Uploadedimages/" + trip + "/" + year + "/" + user + "/"; 
    return View(); 
} 

,這裏是一個樣本我想如何擊中它的網址:

http://localhost:31065/home/show/green/2013/hunt 

我的global.asax中的這些方法都不起作用。我讀過的所有東西都會讓我相信這應該起作用。

我在這裏錯過了什麼?

+0

什麼是404說,它不能準確地找到? – asymptoticFault

+0

我添加了您的第一條路線和您的控制器操作,該URL工作得很好。您在'Views'文件夾內的'Home'文件夾中顯示'Show'視圖,例如'查看\首頁\ Show.cshtml'? – asymptoticFault

+0

是顯示視圖在那裏。有趣的是,如果我只是去/ home/show,頁面會出現。我確信我已經重建瞭解決方案,但仍然無法正常工作。 –

回答

0

這是我RouteConfig,與URL http://localhost:31065/home/show/green/2013/hunt工作:

public class RouteConfig 
{ 
    public static void RegisterRoutes(RouteCollection routes) 
    { 
     routes.IgnoreRoute("{resource}.axd/{*pathInfo}"); 

     routes.MapRoute(
      name: "Show", 
      url: "{controller}/{action}/{trip}/{year}/{user}", 
      defaults: new { controller = "Home", action = "Show" } 
     ); 

     routes.MapRoute(
      name: "Default", 
      url: "{controller}/{action}/{id}", 
      defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional } 
     ); 
    } 
}