【发布时间】:2012-07-26 16:17:20
【问题描述】:
我想同时访问 /Blog 和 /Blog/1,其中“1”是博客的 ID。这是我的代码:
'
' 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
它给出了错误:
“/”应用程序中的服务器错误。
当前对控制器类型的操作“索引”请求 'BlogController' 在以下操作方法之间不明确: System.Web.Mvc.ViewResult Index() 类型 GemcoBlog.GemcoBlog.BlogController System.Web.Mvc.ViewResult GemcoBlog.GemcoBlog.BlogController 类型的索引(Int32)
描述:执行过程中发生了未处理的异常 当前的网络请求。请查看堆栈跟踪以获取更多信息 有关错误的信息以及它在代码中的来源。
异常详细信息:System.Reflection.AmbiguousMatchException: 当前对控制器类型“BlogController”的操作“索引”请求 以下动作方法之间有歧义: System.Web.Mvc.ViewResult Index() 类型 GemcoBlog.GemcoBlog.BlogController System.Web.Mvc.ViewResult GemcoBlog.GemcoBlog.BlogController 类型的索引(Int32)
来源错误:
在执行过程中产生了一个未处理的异常 当前的网络请求。有关原产地和位置的信息 可以使用下面的异常堆栈跟踪来识别异常。
如何重载 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
但是,我得到的错误是:
“/”应用程序中的服务器错误。
参数字典包含参数“id”的空条目 方法“System.Web.Mvc.ViewResult”的不可空类型“System.Int32” 'Blog.Blog.BlogController' 中的索引(Int32)'。一个可选的 参数必须是引用类型、可为空的类型或声明为 一个可选参数。参数名称:参数
描述:执行过程中发生了未处理的异常 当前的网络请求。请查看堆栈跟踪以获取更多信息 有关错误的信息以及它在代码中的来源。
异常详细信息:System.ArgumentException:参数字典 包含不可为空类型的参数“id”的空条目 方法“System.Web.Mvc.ViewResult Index(Int32)”的“System.Int32” '博客.博客.博客控制器'。可选参数必须是 引用类型,可为空的类型,或被声明为可选 范围。参数名称:参数
来源错误:
在执行过程中产生了一个未处理的异常 当前的网络请求。有关原产地和位置的信息 可以使用下面的异常堆栈跟踪来识别异常。
【问题讨论】:
标签: asp.net-mvc vb.net asp.net-mvc-3