zoukankan      html  css  js  c++  java
  • Asp.net core 学习笔记 (操作 url and query params)

    更新 :2018-7-25

    直接添加 query string.

    var resetPasswordLink = QueryHelpers.AddQueryString($"{Request.Scheme}://{Request.Host.Value}{forgotPasswordData.resetPasswordUrl}", "token", token);

    refer : https://benjii.me/2017/04/parse-modify-query-strings-asp-net-core/

    build-in 的功能哦. 

    using Microsoft.AspNetCore.Http.Extensions;
    using Microsoft.AspNetCore.WebUtilities;
    
    public IActionResult About()
    {
        string currentFullUrl = $"{Request.Scheme}://{Request.Host}{Request.Path}{Request.QueryString}";            
        var uri = new Uri(currentFullUrl);
        var baseUri = uri.GetComponents(UriComponents.Scheme | UriComponents.Host | UriComponents.Port | UriComponents.Path, UriFormat.UriEscaped);
        var query = QueryHelpers.ParseQuery(uri.Query);
        var items = query.SelectMany(x => x.Value, (col, value) => new KeyValuePair<string, string>(col.Key, value)).ToList();
        var pageItem = items.FirstOrDefault(i => i.Key == "page"); // 是可以有多个的哦.
        if (pageItem.Key != null) // 判断是否有 
        {
            int page = Convert.ToInt32(pageItem.Value); 
        }
        items.RemoveAll(x => x.Key == "key");  
        items.RemoveAll(x => x.Key == "key" && x.Value == "value");  
        var qb = new QueryBuilder(items);
        qb.Add("newKey", "newValue");
        var newUrl = baseUri + qb.ToQueryString();
        return View();
    }

    part 2 

    UriBuilder uriBuilder = new UriBuilder();
    uriBuilder.Scheme = Request.Scheme;
    uriBuilder.Host = Request.Host.Host;
    if (Request.Host.Port.HasValue)
    {
        uriBuilder.Port = Request.Host.Port.Value;
    }
    uriBuilder.Path = forgotPasswordData.resetPasswordPath;
    var queryBuilder = new QueryBuilder();
    queryBuilder.Add("token", token);
    uriBuilder.Query = queryBuilder.ToString();
    var url = uriBuilder.ToString();

    以上

  • 相关阅读:
    【Offer】[5] 【替换空格】
    【Offer】[3-2] 【不修改数组找出重复的数字】
    【Offer】[3-1] 【找出数组中重复的数字】
    【Spring】 IOC Base
    【Java并发】CallBack和Future模式
    【Java并发】并发队列与线程池
    【Java并发】几个常用API
    linux基础_vi和vim快捷键
    python_字典
    python_列表方法
  • 原文地址:https://www.cnblogs.com/keatkeat/p/9024022.html
Copyright © 2011-2022 走看看