【问题标题】:.NET MVC 5 How can I route a URI with an integer parameter only to /Home/Index?.NET MVC 5 如何将带有整数参数的 URI 仅路由到 /Home/Index?
【发布时间】:2015-08-06 23:08:16
【问题描述】:

我有一个网络应用程序,我需要路由如下 URL: http://example.com/1234567890 到标准的“Home”控制器和“Index”操作(/Home/Index)。

我只是在 RouteConfig 模块中添加了另一个 MapRoute? -- 类似:

    routes.MapRoute(
        name:="Clients",
        url:="{tendigits:int}",
        defaults:=New With {.controller = "Home", .action = "Index", .id = UrlParameter.Optional}
    )

显然上述方法似乎不起作用,因此是我提出问题的原因。提前致谢! (VB.NET 4.6)

编辑:

我在上面得到了 404。如下更改仍会呈现 404:

        routes.MapRoute(
        name:="Clients",
        url:="{controller}/{action}/{id}",
        defaults:=New With {.controller = "Home", .action = "Index", .id = "\d(10)"}
    )

编辑#2 这是Route.Config文件的全部代码:

Imports System
Imports System.Collections.Generic
Imports System.Linq
Imports System.Web
Imports System.Web.Mvc
Imports System.Web.Routing

Public Module RouteConfig
Public Sub RegisterRoutes(ByVal routes As RouteCollection)
    routes.IgnoreRoute("{resource}.axd/{*pathInfo}")

    routes.MapMvcAttributeRoutes    'added

    routes.MapRoute(
        name:="Clients",
        url:="{tendigits}",
        defaults:=New With {.controller = "Home", .action = "Index"},
        constraints:=New With {.tendigits = "\\d{10}"}
    )

    routes.MapRoute(
        name:="Default",
        url:="{controller}/{action}/{id}",
        defaults:=New With {.controller = "Home", .action = "Index", .id = UrlParameter.Optional}
    )

End Sub
End Module

【问题讨论】:

    标签: asp.net-mvc vb.net asp.net-mvc-routing


    【解决方案1】:

    只需将url:="{tendigits:int}" 行替换为url:="{id}",其他一切看起来都很好。

    【讨论】:

    • 感谢@Sandip Patel 的回复。我尝试了使用和不使用 :int 约束(我需要这样做)但它不起作用 - 至少不是使用 localhost,但我认为这并不重要。还有其他想法吗?
    • @HumbleBeginnings,请尝试下面的代码routes.MapRoute( name: "Clients", url: "{tendigits}", defaults: new { controller = "Home", action = "Index" }, constraints: new { tendigits = "\\d{10}" } ); 它必须通过 10 位否则会给出 404
    • 运气不好。这是我的 VB 等价物——我错过了什么吗? routes.MapRoute( name:="Clients", url:="{tendigits}", defaults:=New With {.controller = "Home", .action = "Index"}, constraints:=New With {.tendigits = "\\d{10}"} )我以为只需要一个“\”但也许我错了......
    • 我将其置于我认为正确的默认值之上。常规处理适当地通过这条新路线,但我仍然得到 10 位数的 404。
    • @HumbleBeginnings 你是对的,它在 vb.net 中不起作用。但有些如何在 c# 中工作。我对此有替代计划。来自 RouteConfig 的注释 route="Clients"。并在 Controller 中的 Action 处添加路由属性,如下所示。我在 vb 项目中对其进行了测试,并且工作正常。 <Route("{tendigits:regex(^\d{10}$)}")> Function Index(ByVal tendigits As Integer) As ActionResult Return View() End Function
    【解决方案2】:

    一时兴起,我决定尝试另一种已被证明是成功的语法(至少到目前为止):

        routes.MapRoute(
            name:="Clients",
            url:="{tendigits}",
            defaults:=New With {.controller = "Home", .action = "Index", .tendigits = ""},
            constraints:=New With {.tendigits = "\d{10}"}
        )
    

    如果有人发现我可能遗漏的这种方法有什么问题,请提出建议。

    【讨论】:

      猜你喜欢
      • 2021-10-13
      • 2021-09-28
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-10-13
      • 2015-04-17
      相关资源
      最近更新 更多