c#代碼小集

一、字符串[Uri]轉換出RouteData

private RouteData UriToRouteData(Uri uri)
{
    var query = uri.Query;
    if (uri.Query.Length > 0)
    {
        query = query.Substring(1, uri.Query.Length - 1);
    }
    var request = new HttpRequest("", uri.AbsoluteUri, query);
    var routeData= Url.RouteCollection.GetRouteData(new HttpContextWrapper(new HttpContext(
        request,
        new HttpResponse(null))));

    if (request.QueryString.Count > 0 && routeData != null)
    {
        foreach (string key in request.QueryString.Keys)
        {
            routeData.Values.Add(key, request.QueryString[key]);
        }
    }

    return routeData;
}

//調用
//RouteData routeData = UriToRouteData(Request.UrlReferrer);

 

posted @ 2018-03-26 22:06  天际翔龙  阅读(128)  评论(0编辑  收藏  举报