有没有办法将可选参数发送给某个动作?

时间:2022-03-21 10:55:58

Can I send optional parameters (empty strings, null int?'s etc) to an action through a GET request in asp.net mvc? (one sentence question!)

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

1 个解决方案

#1


2  

You can do optional parameters with the routing table fairly easily, just specify the defaults in the route of the global.cs file.

您可以非常轻松地使用路由表执行可选参数,只需指定global.cs文件路由中的默认值即可。

So for a search page with an optional query and page you would have something like

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

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

Default page for your search is then 1.

然后搜索的默认页面为1。

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

这个例子可以在Scott Gu的博客上找到。

#1


2  

You can do optional parameters with the routing table fairly easily, just specify the defaults in the route of the global.cs file.

您可以非常轻松地使用路由表执行可选参数,只需指定global.cs文件路由中的默认值即可。

So for a search page with an optional query and page you would have something like

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

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

Default page for your search is then 1.

然后搜索的默认页面为1。

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

这个例子可以在Scott Gu的博客上找到。