標籤:
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>
C# Asp.net MVC 分頁時傳參情境 動態產生QueryString