1.获取完整网址URL

方法一:先引用“using Microsoft.AspNetCore.Http.Extensions;”,然后直接用“Request.GetDisplayUrl();”

方法二:后来参考 Microsoft.AspNetCore.Rewrite 的源代码,写了一个扩展方法实现

namespace Microsoft.AspNetCore.Http
{
    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();
        }
    }
}

 

2.获取网站在服务器的物理路径

    public class HomeController : Controller
    {
        IHostingEnvironment env;
        public HomeController(IHostingEnvironment _env) { env = _env; }

        public IActionResult Index()
        {
            //https://localhost:44359/home/index
            string url = HttpContext.Request.GetDisplayUrl();

            //D:\\Visual Studio 2017\\tourism_app\\ZhiRen.Tourism.UI\\bin\\Debug\\netcoreapp2.1\\
            string path = System.AppContext.BaseDirectory;

            //D:\\Visual Studio 2017\\tourism_app\\ZhiRen.Tourism.UI
            string siteRoot = env.ContentRootPath;

            //D:\\Visual Studio 2017\\tourism_app\\ZhiRen.Tourism.UI\\wwwroot
            string wwwroot = env.WebRootPath;

            //D:\\Visual Studio 2017\\tourism_app\\ZhiRen.Tourism.UI\\bin\\Debug\\netcoreapp2.1
            var basePath = Path.GetDirectoryName(typeof(Program).Assembly.Location);
            
            return View();
        }
    }

  

相关文章:

  • 2022-12-23
  • 2021-11-02
  • 2022-12-23
  • 2021-08-26
  • 2022-12-23
猜你喜欢
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2021-10-04
  • 2022-01-02
  • 2022-12-23
  • 2021-11-15
相关资源
相似解决方案