我想訪問/ 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.
這難道不意味着URL必須是博客/博客/ 1 /顯示/ 1呢?如果我將它們合併成一種方法會怎麼樣?我用新代碼更新了這個問題,但它也給我一個錯誤。 – user1477388 2012-07-26 16:52:46
你可以在你的路由配置中使用Global.asax中的'Show'動作默認值:'action =「Show」'而不是'action =「Index」'。現在你可以使用'/ Blog/123'。不,不要將這2個組合成一個動作。那將是糟糕的設計決定。 – 2012-07-26 17:07:46