MVC4 Visual基本路由和参数

时间:2014-07-16 06:28:46

标签: vb.net asp.net-mvc-4 parameters routes

我知道已经问过类似的问题,但这不是我的问题的解决方案。我的MVC4 VB应用程序中的路由和参数有问题。问题是当我去/产品询问参数时,这是正常的。

但/ product?id = 0正在运行且/ product / 1无效。

IIS在最后一个示例中抛出了“无法找到资源”

我和之前的项目有同样的问题,我做了一些事情,但我不记得我做了什么。 我知道路线应该可以正常工作但是......

路线:

 routes.MapRoute("product", "product/{id}/{name}", New With {.controller = "product", .action = "Index", .id = UrlParameter.Optional, .name= UrlParameter.Optional})

控制器:

 Function Index(ByVal id As Integer, Optional name As String = "") As ActionResult
        Return View("Product")
    End Function

2 个答案:

答案 0 :(得分:2)

我发现了问题,在我有

的Application_Start()方法中
 RouteConfig.RegisterRoutes(RouteTable.Routes)

并将其更改为

RegisterRoutes(RouteTable.Routes)`

现在它可以工作,它接收MVC根据视图文件夹和控制器自动创建路径。

答案 1 :(得分:0)

这里的问题是由多个可选参数引起的。由于它们都是可选的,因此路由引擎无法知道1是id还是名称

要解决此问题,您必须定义单独的路线,例如

routes.MapRoute(“product-id”,“product / {id}”,New With {.controller =“product”,。action =“Index”,。id = UrlParameter.Optional})

routes.MapRoute(“product-id-and-name”,“product / {id} / {name}”,New With {.controller =“product”,。action =“Index”})

有关详细说明,请参阅

http://haacked.com/archive/2011/02/20/routing-regression-with-two-consecutive-optional-url-parameters.aspx/