2013-08-22 122 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 } 
     ); 
    } 
}