2011-11-02 147 views
0

让自己有问题,开始在MVC3路由问题...未找到资源 - 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并试图路由调试

现在说有不匹配!但低于它说,它目前的请求匹配......

enter image description here

回答

0
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")} 
) 
+0

更改和错误仍然存​​在......我假设'_me'是因为'Me'是VB中的一个关键字,我只需单击create controller并将其命名为我。 – jondavidjohn

+0

更改了代码以反映仍然出错的建议。 – jondavidjohn

+0

@jondavidjohn,在你的路线中使用'.controller =“我”而不是'.controller =“MeController”'。你似乎编辑了你最初的问题。 –

2

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

+0

改变了这一切,同一个问题的http:// cl.ly/0C0I2y0z1W3y3L0k1014 – jondavidjohn

+0

尝试[route debugger](http://haacked.com/archive/2008/03/13/url-routing-debugger.aspx)。 – SLaks

+0

所以它说定义的路由匹配当前的请求,但它也说没有匹配:/ http://cl.ly/1y3u0O152o2k14281U2u – jondavidjohn

0

添加Public到你的动作方法。

您还需要通过JsonRequestBehavior.AllowGet

+0

传递它在哪里?还是为了什么? – jondavidjohn

+1

致'Json(...)'。 – SLaks

+0

在完成所有建议的编辑后,将控制器代码更新为当前,并具有相同的错误。谢谢,顺便说一句。 – jondavidjohn