【问题标题】:Is there a way to send optional parameters to an action?有没有办法将可选参数发送到操作?
【发布时间】:2008-11-20 01:52:38
【问题描述】:

我可以通过 asp.net mvc 中的 GET 请求将可选参数(空字符串、null int?'s 等)发送到操作吗? (一句话问题!)

【问题讨论】:

    标签: asp.net-mvc optional-parameters


    【解决方案1】:

    你可以很容易地用路由表做可选参数,只需在 global.cs 文件的路由中指定默认值。

    因此,对于带有可选查询和页面的搜索页面,您将拥有类似

    RouteTable.Routes.Add(new Route
    {
        Url = "Search/[query]/[page]",
        Defaults = new { controller="Search", action="Results", page=1 },
        RouteHandler = typeof(MvcRouteHandler)
    });
    

    您搜索的默认页面为 1。

    This example is found here on Scott Gu's blog.

    【讨论】:

    • 是的,但是我的搜索 url 有大约 15 个可选参数。并且可选并不意味着默认,它意味着可选。我当然可以同意某个条款,然后我的控制器在找到时会丢弃。
    猜你喜欢
    • 2021-01-03
    • 1970-01-01
    • 1970-01-01
    • 2022-01-03
    • 2017-02-08
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多