ASP.Net MVC:通过路由和视图方法请求变量
基本上,如果我想创建一个带有分页的搜索页面,我需要一个像这样的网址:
/Topics/Index?search=hi&page=1
我似乎不知道如何:
A)设置没有搜索和页面1的默认路由 /Topics/Index?page=1 甚至 /Topics/Index?search=&page=1
B) 使用 View 方法执行相同的操作
我确实看到,如果我在控件上有一个方法:
Index(String search, Int32? page)
并使用 url:
/Topics/Index?search=hi&page=1 or /Topics/Index?search=hi
它给了我我想要的方法。 我只需要一种方法来获取主题控制器的默认路由,以使用所述请求变量创建默认 url。 我只是不认为
/Topics/Index/hi/1
有利于搜索网址,主要是因为不能保证我会有搜索词或页面,所以它最终可能会像:
/Topics/Index/1
Basically if I wanted to make something a search page with paging I would need a url like:
/Topics/Index?search=hi&page=1
What I can't seem to figure out is how to:
A) Set a default route with no search and page 1
/Topics/Index?page=1 or even /Topics/Index?search=&page=1
B) use the View method to do the same
I do see that if I have a method on the control:
Index(String search, Int32? page)
And use the url:
/Topics/Index?search=hi&page=1 or /Topics/Index?search=hi
It gives me what I want in the method. I just need a way to get a default route for the Topic controller to create a default url with said request variables. I just don't think that
/Topics/Index/hi/1
Is conducive to a search url, mostly because there's no guarantee I'll have search terms or a page so it could end up like:
/Topics/Index/1
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您在 RouteValueDictionary 中传递的任何未映射到 Url 部分的内容都将作为查询字符串参数添加。 所以你可以这样做:
Anything you pass in the RouteValueDictionary that doesn't map to a part of your Url will get added as a querystring parameter. So you can do:
所以基本上我通过在控制器上设置默认值来处理非值。 但不确定这是最好的主意。
在 GLobal.asax 中:
在控制器上:
So basically I resorted to handling the non values by setting up defaults on the controller. Not sure this is the best idea though.
In GLobal.asax:
On the Controller: