zoukankan      html  css  js  c++  java
  • asp.net core获取当前请求的完整url

    asp.net core获取当前请求的完整url

    首先需要在控制器项目中使用NuGet引入 Microsoft.AspNetCore.Http.Abstractions包。

    然后有两种获取当前URL的方式。

    方法1,在控制器里面,string displayUrl = HttpContext.Request.GetDisplayUrl();

    方法2,定义一个HttpRequest的扩展类。

    using Microsoft.AspNetCore.Http.Extensions;
    
    namespace demo1.tools
    {
        public static class HttpRequestExtensions
        {
            public static string GetAbsoluteUri(this HttpRequest request)
            {
                return new StringBuilder()
                    .Append(request.Scheme)
                    .Append("://")
                    .Append(request.Host)
                    .Append(request.PathBase)
                    .Append(request.Path)
                    .Append(request.QueryString)
                    .ToString();
            }
        }
    }

    调用方式:

    方法1,直接调用 Request.GetDisplayUrl()方法。
    string displayUrl = HttpContext.Request.GetDisplayUrl();
    方法2 调用扩展方法
    string requestUrl = HttpRequestExtensions.GetAbsoluteUri(HttpContext.Request);

    using demo1.tools;
    using Microsoft.AspNetCore.Http.Extensions;
    
    namespace demo1.xTwoMvc.Controllers
    {
        public class HomeController : Controller
        {
            public IActionResult Index()
            {
                //方法1 直接调用Request.GetDisplayUrl()方法
                string displayUrl = HttpContext.Request.GetDisplayUrl();  
                //方法2 调用扩展方法
                string requestUrl = HttpRequestExtensions.GetAbsoluteUri(HttpContext.Request);
                base.ViewData["displayUrl"] = displayUrl;
                base.ViewData["requestUrl"] = requestUrl; 
                return View();
            }
        }
    }


  • 相关阅读:
    pandas read_excel 产生 Unnamed:0 列
    python 打印输出百分比符号%
    python 内存回收
    python 编码问题
    python 判断 txt 编码方式
    python 二维list取列
    python 两个list 求交集,并集,差集
    pandas Timestamp的用法
    Dataframe 取列名
    Dataframe 新增一列, apply 通用方法
  • 原文地址:https://www.cnblogs.com/yyee/p/12825802.html
Copyright © 2011-2022 走看看