生成基于URL关闭当前URL查询字符串和维护 [英] Generate URL based off of the current URL and maintain QueryString
问题描述
我有用于显示项目列表的局部视图,我使用在几个不同的地方这局部视图。这里面局部视图我使用分页程序 -
@ Html.PagedListPager(型号,页= GT; Url.Action(空,新的{页=页}))
这导致分页程序显示网页网址的任何行动,并查看我已经在看。
问题是,我的搜索页面上我使用的搜索字符串的查询字符串,Url.Action方法不包括现有的查询字符串参数。
而不是/搜索S =喇嘛和放大器;?页= 3我结束了/搜索页= 3
我如何使用现有的查询字符串生成一个URL?
编辑:
下面是我的code
公共静态无效的RegisterRoutes(RouteCollection路线)
{
routes.IgnoreRoute({}资源个.axd / {*} PATHINFO);
routes.Add(
搜索,
新SearchRoute(搜索,新MvcRouteHandler())
{
默认值=新RouteValueDictionary(
新{控制器=搜索,行动=索引})
});
routes.MapRoute(
默认,
{控制器} / {行动} / {ID}
新{控制器=呼叫,行动=索引,ID = UrlParameter.Optional}
); } 公共类SearchRoute:路线
{
公共SearchRoute(字符串URL,IRouteHandler routeHandler)
:基地(URL,routeHandler)
{
} 公众覆盖VirtualPathData GetVirtualPath(RequestContext的的RequestContext,RouteValueDictionary值)
{ System.Diagnostics.Debug.WriteLine(URL); 如果(HttpContext.Current!= NULL)
{ 字符串s = HttpContext.Current.Request.QueryString [S]; 如果(!string.IsNullOrEmpty(多个))
values.Add(S,S); } 返回base.GetVirtualPath(RequestContext的,值);
}
}
使用定制的路线,你可以preserve查询字符串,因为大多数URL生成逻辑使用路由生成URL。在这种情况下,我检查叫XXX,添加入途径,如果它存在,你可以把它,如果你喜欢更通用的查询字符串Request对象。
使用的System.Web;
使用System.Web.Routing;公共类preserveQueryStringRoute:路线
{
公共preserveQueryStringRoute(字符串URL,IRouteHandler routeHandler)
:基地(URL,routeHandler)
{
} 公众覆盖VirtualPathData GetVirtualPath(RequestContext的的RequestContext,RouteValueDictionary值)
{
如果(HttpContext.Current!= NULL)
{
值=新RouteValueDictionary(值); //这是错误修复! 如果(!string.IsNullOrEmpty(HttpContext.Current.Request.QueryString [XXX))
values.Add(XXX,HttpContext.Current.Request.QueryString [XXX]); } VAR路径= base.GetVirtualPath(RequestContext的,值);
返回路径;
}
}
登记按正常路线在global.ascx(虽然可能不是这样一个普通的比赛,因为我有下文)
routes.Add(
默认,
新的preserveQueryStringRoute({控制器} / {行动} / {ID},新MvcRouteHandler())
{
默认值=新RouteValueDictionary(
新{控制器=家,行动=索引,ID = UrlParameter.Optional})
});
编辑:
好吧,这里是一个更新..其一个bug修复,以及如何注册一个路径的例子(见纠正code以上bug修复)
下面是如何注册:
routes.Add(
搜索,
新SearchRoute(搜索,新MvcRouteHandler())
{
约束=新RouteValueDictionary(
新{控制器=搜索,行动=索引})
});
I have a partial view for displaying a list of items, I use this partial view in several different places. Inside this partial view I use a paginator -
@Html.PagedListPager(Model, page => Url.Action(null, new { page = page }))
This results in the paginator showing page urls for whatever Action and View i'm already looking at.
Problem is, on my search page I use a query string for the search string, and the Url.Action method does not include existing querystring parameters.
Instead of /Search?s=bla&page=3 I end up with /Search?page=3
How can I generate a url using the existing query string?
Edit:
Here is my code
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);
}
}
Using a custom route, you can preserve the query string because most Url generation logic uses the route to generate the URL. In this case I am checking the Request object for a query string called XXX and adding it into the route if it exists, you can make it more generic if you like.
using System.Web;
using System.Web.Routing;
public class PreserveQueryStringRoute : Route
{
public PreserveQueryStringRoute(string url, IRouteHandler routeHandler)
: base(url, routeHandler)
{
}
public override VirtualPathData GetVirtualPath(RequestContext requestContext, RouteValueDictionary values)
{
if(HttpContext.Current != null)
{
values = new RouteValueDictionary(values); //this is the bug fix!
if (!string.IsNullOrEmpty(HttpContext.Current.Request.QueryString["XXX"]))
values.Add("XXX", HttpContext.Current.Request.QueryString["XXX"]);
}
var path = base.GetVirtualPath(requestContext, values);
return path;
}
}
Register the route as per normal in the global.ascx (though probably not for such a generic match as I have below)
routes.Add(
"Default",
new PreserveQueryStringRoute("{controller}/{action}/{id}", new MvcRouteHandler())
{
Defaults = new RouteValueDictionary(
new { controller = "Home", action = "Index", id = UrlParameter.Optional })
});
Edit:
Ok, here's an update.. its a bug fix, and an example of how to register the route (See corrected code above for bug fix)
Here's how to register it:
routes.Add(
"Search",
new SearchRoute("Search", new MvcRouteHandler())
{
Constraints = new RouteValueDictionary(
new { controller = "Search", action = "Index" })
});
这篇关于生成基于URL关闭当前URL查询字符串和维护的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!