1. 获得url
2.解析query string,将外部传入的匿名对象的key/value添加进去
3. 添加该页面特殊的key/value
4.合并url
var raw = HttpContext.Current.Request.Url.AbsoluteUri; string[] separateURL = raw.Split(‘?‘); //if found "?" parse query string else return empty HttpValueCollection var queryString = separateURL.Length == 2 ? HttpUtility.ParseQueryString(separateURL[1]) : HttpUtility.ParseQueryString(""); //used to add additional query string key/value (pass anonymous object) //e.g : //@Html.DisplayFor(x => x.Paging, new // { // AdditionalViewData = new { // Model.OrdersFilter, Model.SearchKeyword, Model.PickupDateRange, // Model.RequestDateRange, Model.DeliveryDateRange // } //pass in from outside if (ViewData["AdditionalViewData"] != null) { foreach (var str in ViewData["AdditionalViewData"].GetType().GetProperties()) { queryString[str.Name] = (str.GetValue(ViewData["AdditionalViewData"], null) ?? string.Empty).ToString(); } } //one more specific query strings on this page @{ queryString[xxx] = yyy; } //// <a href="@string.Format("{0}?{1}", separateURL[0], queryString)">something friendly</a>
时间: 2024-11-09 21:32:09