获取当前URL就可以在Program.cs / startup.cs在ASP.NET核心

问题描述 投票:-1回答:1

目标:要得到尽快上运行的ASP.NET 2.2核心Web应用程序在浏览器的URL。

我试过几乎每样Startup.cs内两轮牛车的,它确保您可以使用DI用于注册IHttpContextAccessor以访问HttpContext

有人会说使用

var url = HttpContext?.Request?.GetDisplayUrl();

您可以在控制器使用,但是如果你去定义你看到的HttpContext是MVC中等等的ControllerBase未来..

似乎这件事,没有解决方案多个岗位。

  1. 我看到建立中间件 - 伟大的,但我不知道如何真正做到这一点
  2. 我看到关于中间件的文章并调用Invoke方法,但是如何以及在何处等..? Current URL in ASPCore Middleware?
  3. 好像我只是想什么,我不得不在“经典” .NET与URL等的Global.asax ..

我看到,程序调用Startup.cs与.UseStartup<Startup>();是否有可能获得访问能够最终目标得到像http://localhost:4444网址

我所想要的 ...

var url = HttpContext?.Request?.GetDisplayUrl(); 

尽快展示我的URL作为在类库/启动.NET核心/ Program.cs中都会让我看看URL像http://localhost:4444

c# asp.net-core asp.net-core-mvc url-routing asp.net-mvc-routing
1个回答
1
投票

对于处理请求时,你可以尝试qazxsw POI。

一个简单的中间件象下面这样:

ASP.NET Core Middleware

对于使用 public class Startup { //rest code // This method gets called by the runtime. Use this method to configure the HTTP request pipeline. public void Configure(IApplicationBuilder app, IHostingEnvironment env) { app.Use((context,next) => { var url = context.Request.GetDisplayUrl(); return next.Invoke(); }); //rest code } } ,加

GetDisplayUrl()
© www.soinside.com 2019 - 2024. All rights reserved.