gpt4 book ai didi

asp.net-mvc - 在 MVC Controller 中重载 Index() 方法

转载 作者:行者123 更新时间:2023-12-01 11:50:29 24 4
gpt4 key购买 nike

我想同时访问/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

它给出了错误:

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.

最佳答案

您不能在同一个 Controller 上使用同一个 HTTP 谓词访问 2 个操作。因此,要么更改操作名称,要么您将不得不使用不同的 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 访问。所以在这种情况下只需重命名它。遵循标准的 RESTful 约定,您可以使用 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

关于asp.net-mvc - 在 MVC Controller 中重载 Index() 方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11673633/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com