2011-09-02 169 views
0

我控制器博客ASP.NET MVC 3.0路由

我有行動Index()Show(stirng id)

Index()顯示所有帖子和Show(sting id)顯示單後

我要地圖Blog/Show/id到性反應上Blog/id

所以我去了Global.asxc並做到了這一點:

routes.MapRouteLowercase(
       "Blog", 
       "Blog/{id}", 
       new { controller = "Blog", action = "Show", id = UrlParameter.Optional } 
      ); 

但它是接縫不能工作,可能有人可能會幫助?

回答

1

你可以有以下途徑:

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

    routes.MapRoute(
     "Blog", 
     "Blog/{id}", 
     new { controller = "Blog", action = "Show" } 
    ); 

    routes.MapRoute(
     "Default", 
     "{controller}/{action}/{id}", 
     new { controller = "Blog", action = "Index", id = UrlParameter.Optional } 
    ); 
} 

現在:

  • /Blog/123將映射到BlogController/Show(123)行動
  • //Blog將映射到BlogController/Index行動
+0

我已經有'default'路線'routes.MapRouteLowercase(默認 「」{}控制器/ {動作}/{id}「,新的{controller =」Home「,action =」Index「,id = UrlParameter.Optional});' – jason

+0

@jason,然後一切正常。 'MapRouteL owercase'?它不是ASP.NET MVC標準。你寫的東西?考慮到它的名字,我懷疑它要求你所有的路由都是小寫的,所以你不能擁有'Blog/id',你可能需要'blog/id'。 –

+1

我做了一些改變,我在這裏發佈另一個問題http://stackoverflow.com/questions/7283622/asp-net-mvc-3-0-routing-behaviour可能你可能看看,我決定不使用顯示( )的行動,但得到一些麻煩 – jason

0

您要創建的新路線應該是第一個註冊的路線,否則默認路線將會啓動並處理請求。

確保

routes.MapRouteLowercase(
       "Blog", 
       "Blog/{id}", 
       new { controller = "Blog", action = "Show", id = UrlParameter.Optional } 
      ); 

在Global.asax中的RegisterRoutes

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

之前放置