我有一个显示项目列表的局部视图,我在几个不同的地方使用了这个局部视图。这里面局部视图我使用分页程序 -根据当前URL生成URL并维护QueryString
@Html.PagedListPager(Model, page => Url.Action(null, new { page = page }))
这将导致分页程序显示的网页网址为任何行动和查看我已经在看。
问题是,在我的搜索页面上,我使用查询字符串作为搜索字符串,而Url.Action方法不包括现有查询字符串参数。
而不是/搜索?S =喇嘛&页= 3我结束了/搜索页→= 3
我怎样才能利用现有的查询字符串生成一个URL?
编辑:
这里是我的代码
public static void RegisterRoutes(RouteCollection routes)
{
routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
routes.Add(
"Search",
new SearchRoute("Search", new MvcRouteHandler())
{
Defaults = new RouteValueDictionary(
new { controller = "Search", action = "Index" })
});
routes.MapRoute(
"Default",
"{controller}/{action}/{id}",
new { controller = "Call", action = "Index", id = UrlParameter.Optional }
);
}
public class SearchRoute : Route
{
public SearchRoute(string url, IRouteHandler routeHandler)
: base(url, routeHandler)
{
}
public override VirtualPathData GetVirtualPath(RequestContext requestContext, RouteValueDictionary values)
{
System.Diagnostics.Debug.WriteLine(Url);
if (HttpContext.Current != null)
{
string s = HttpContext.Current.Request.QueryString["s"];
if (!string.IsNullOrEmpty(s))
values.Add("s", s);
}
return base.GetVirtualPath(requestContext, values);
}
}
神奇。你达人。如果有其他人使用此项,请记住将呼叫路由添加到默认路由上方。 – NoPyGod 2012-01-19 01:44:44
嗯等一下..这可能不能正常工作 – NoPyGod 2012-01-19 01:57:06
它将查询字符串放在我的搜索页面上生成的所有url上。这可以修复吗?我只需要搜索网址上的查询字符串。 – NoPyGod 2012-01-19 02:00:38