2012-07-26 23 views
3

我想訪問/ Blog和/ Blog/1,其中「1」是博客的ID。這裏是我的代碼:MVC控制器中的重載索引()方法

' 
    ' GET: /Blog/ 

    Function Index() As ViewResult 
     Return (View(db.Blogs.ToList())) 
    End Function 

    ' 
    ' GET: /Blog/(Integer) 

    Function Index(id As Integer) As ViewResult 
     Dim blog As Blog = db.Blogs.Find(id) 
     Return View("Details", "_MyLayout", blog) 
    End Function 

它給人的錯誤:

Server Error in '/' Application.

The current request for action 'Index' on controller type 'BlogController' is ambiguous between the following action methods: System.Web.Mvc.ViewResult Index() on type GemcoBlog.GemcoBlog.BlogController System.Web.Mvc.ViewResult Index(Int32) on type GemcoBlog.GemcoBlog.BlogController

Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.

Exception Details: System.Reflection.AmbiguousMatchException: The current request for action 'Index' on controller type 'BlogController' is ambiguous between the following action methods: System.Web.Mvc.ViewResult Index() on type GemcoBlog.GemcoBlog.BlogController System.Web.Mvc.ViewResult Index(Int32) on type GemcoBlog.GemcoBlog.BlogController

Source Error:

An unhandled exception was generated during the execution of the current web request. Information regarding the origin and location of the exception can be identified using the exception stack trace below.

我如何能超載Index()方法?

編輯:

我也想他們,像這樣結合:

' 
    ' GET: /Blog/ 

    Function Index(id As Integer) As ViewResult 
     If (id) Then 
      Dim blog As Blog = db.Blogs.Find(id) 
      'Return View(blog) 
      Return View("Details", "_MyLayout", blog) 
     Else 
      Return (View(db.Blogs.ToList())) 
     End If 
     'Return View(db.Blogs.Where(Function(x) x.Name = "Test").ToList()) 
    End Function 

但是,我得到的錯誤是:在 '/' 應用

服務器錯誤。

The parameters dictionary contains a null entry for parameter 'id' of non-nullable type 'System.Int32' for method 'System.Web.Mvc.ViewResult Index(Int32)' in 'Blog.Blog.BlogController'. An optional parameter must be a reference type, a nullable type, or be declared as an optional parameter. Parameter name: parameters

Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.

Exception Details: System.ArgumentException: The parameters dictionary contains a null entry for parameter 'id' of non-nullable type 'System.Int32' for method 'System.Web.Mvc.ViewResult Index(Int32)' in 'Blog.Blog.BlogController'. An optional parameter must be a reference type, a nullable type, or be declared as an optional parameter. Parameter name: parameters

Source Error:

An unhandled exception was generated during the execution of the current web request. Information regarding the origin and location of the exception can be identified using the exception stack trace below.

回答

2

對同一個控制器不能有2個動作可以用同一個HTTP動詞訪問。因此,要麼更改操作名稱,要麼必須使用不同的HTTP動詞消除歧義。例如:

<HttpPost> 
Function Index(id As Integer) As ViewResult 
    Dim blog As Blog = db.Blogs.Find(id) 
    Return View("Details", "_MyLayout", blog) 
End Function 

但是由於其他操作似乎也正在提取數據,我猜你不希望僅使其POST可訪問。所以在這種情況下重命名它。堅持標準的REST風格的約定,你可以使用Index返回的資源列表和Show返回一個特定的資源:

Function Index() As ViewResult 
    Return (View(db.Blogs.ToList())) 
End Function 

' 
' GET: /Blog/(Integer) 

Function Show(id As Integer) As ViewResult 
    Dim blog As Blog = db.Blogs.Find(id) 
    Return View("Details", "_MyLayout", blog) 
End Function 
+0

這難道不意味着URL必須是博客/博客/ 1 /顯示/ 1呢?如果我將它們合併成一種方法會怎麼樣?我用新代碼更新了這個問題,但它也給我一個錯誤。 – user1477388 2012-07-26 16:52:46

+0

你可以在你的路由配置中使用Global.asax中的'Show'動作默認值:'action =「Show」'而不是'action =「Index」'。現在你可以使用'/ Blog/123'。不,不要將這2個組合成一個動作。那將是糟糕的設計決定。 – 2012-07-26 17:07:46

1

由於它不是空的,它會自動將您在默認情況下提供的ID。使Id爲可空的整數,並且它適用於這兩個URL。

功能指數(ID作爲Nullabe(整數))作爲的ViewResult

2

有一對夫婦,你可以做到這一點的方式。最簡單的方法是將第一個方法重命名爲「ShowBlog」或任何你想要的,然後在你的global.asax中設置一個路由,它不通過參數路由到/ Blog路由。

例如(在C#):

routes.MapRoute("Blog", "Blog", new { controller = "Blog", action = "ShowBlog" }); 

確保圖路線自帶的默認路由之前。

要使第二種方法有效,您需要將id設爲空,然後在您的方法中檢查null。

0

小修改Erik的職位,使其工作(我用MVC4)

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