C# 如何獲取Url的host以及是否是http


參考資料:https://sites.google.com/site/netcorenote/asp-net-core/get-scheme-url-host

Example there's an given url: http://localhost:4800/account/login

獲取整個url地址:

在頁面(cstml)中 

Microsoft.AspNetCore.Http.Extensions.UriHelper.GetDisplayUrl(Context.Request);

在 Controller 中

Microsoft.AspNetCore.Http.Extensions.UriHelper.GetDisplayUrl(Request);

獲取請求的方式(scheme:http/https):http

In asp.net 4.6 -> Request.Url.Scheme

in .net core -> Context.Request.Scheme (cshtml) , in Controller -> Request.Scheme

獲取域名(不帶端口號)[Get the host]:

In asp.net 4.6 -> Request.Url.Host

in .net core -> Context.Request.Host.Host (cshtml) , in Controller -> Request.Host.Host

獲取域名(帶端口號)[Get the host]: localhost:4800

In asp.net 4.6 ->

in .net core -> Context.Request.Host.Value (cshtml) , in Controller -> Request.Host.Value

獲取路徑(Get the path): /account/login

In asp.net 4.6:

In .net core: @Context.Request.Path (cshtml)

獲取端口號(Get port): 4800 (if a url contains port)

In asp.net 4.6: Request.Url.Port

In .net core: @Context.Request.Host.Port (cshtml) , in Controller -> Request.Host.Port


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM