【问题标题】:Routing issue... Resource not found - MVC3路由问题...找不到资源 - MVC3
【发布时间】:2011-11-02 19:28:54
【问题描述】:

在开始使用 MVC3 时遇到问题

尝试在Global.asax这里定义一个简单的路由

routes.MapRoute( _
  "MeGet", _
  "me", _
  New With {.controller = "MeController", .action = "Show"}, _
  New With {.httpMethod = New HttpMethodConstraint("GET")}
)

routes.MapRoute( _
  "MePut", _
  "me", _
  New With {.controller = "MeController", .action = "Update"}, _
  New With {.httpMethod = New HttpMethodConstraint("PUT")}
)

而我的控制器如下。

Public Class MeController
  Inherits System.Web.Mvc.Controller

  '
  ' GET: /me
  Public Function Show() As ActionResult
    Dim stuff = {"Hello", "World"}

    Return Json(stuff, JsonRequestBehavior.AllowGet)
  End Function

  '
  ' PUT: /me
  Public Function Update() As ActionResult

    Return View()
  End Function

End Class

我得到的只是……

找不到资源。

没有堆栈跟踪。

以下建议

将控制器更改为_me 并尝试了路由调试器

现在它说 NO MATCH! 但下面说它与当前请求匹配...

【问题讨论】:

    标签: vb.net json asp.net-mvc-3 rest routing


    【解决方案1】:

    您需要在控制器名称中包含_.controller = "_me"

    【讨论】:

    【解决方案2】:
    Public Class _me
        Inherits System.Web.Mvc.Controller
    

    必须变成:

    Public Class MeController
        Inherits System.Web.Mvc.Controller
    

    在 ASP.NET MVC 约定中,控制器类名称以Controller 为后缀。我不知道你为什么在你的控制器名称前加上一个 _,这是违反约定的,但如果你决定保留它,你也必须在你的路由定义中反映这一点。

    也在你的路线中替换:

    .controller = "MeController" 
    

    与:

    .controller = "Me"
    

    这样您的路由定义如下所示:

    routes.MapRoute( _
      "MeGet", _
      "me", _
      New With {.controller = "Me", .action = "Show"}, _
      New With {.httpMethod = New HttpMethodConstraint("GET")}
    )
    
    routes.MapRoute( _
      "MePut", _
      "me", _
      New With {.controller = "Me", .action = "Update"}, _
      New With {.httpMethod = New HttpMethodConstraint("PUT")}
    )
    

    【讨论】:

    • 已更改且错误仍然存​​在...我假设_me 是因为Me 是VB 中的关键字,我只是单击了创建控制器并将其命名为我。
    • 更改了有问题的代码以反映仍然出错的建议。
    • @jondavidjohn,在您的路线中使用.controller = "Me" 而不是.controller = "MeController"。您似乎已经编辑了最初的问题。
    • 问题中的代码现在反映了当前状态,但仍然出错...
    • @jondavidjohn,在您的路线中。MapRoute 将.controller = "MeController" 替换为.controller = "Me"!!!从路由中删除控制器后缀并将其保留在控制器名称类中。我已经更新了我的答案以更清楚地说明。
    【解决方案3】:

    Public 添加到您的操作方法中。

    您还需要传递JsonRequestBehavior.AllowGet

    【讨论】:

    • 在所有建议的编辑后将控制器代码更新为当前代码,但错误相同。谢谢,顺便说一句。
    猜你喜欢
    • 2014-01-05
    • 1970-01-01
    • 1970-01-01
    • 2017-02-19
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-03-22
    相关资源
    最近更新 更多