在 ASP.NET 中,获取请求的基本 URl 的最快方法是什么? [英] In ASP.NET, what is the quickest way to get the base URl for a request?

查看:19
本文介绍了在 ASP.NET 中,获取请求的基本 URl 的最快方法是什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在 ASP.NET 中是否有快速获取方案、主机、端口(如果不是 80)和应用程序路径的方法?

Is there a fast way to get the scheme, host, port (only if not 80) and application path in ASP.NET?

据我所知,我需要组装以下部分:

As far as I know, I need to assemble the following pieces:

  • Request.Url.Scheme
  • Request.Url.SchemeDelimiter
  • Request.Url.Authority(尽管它可能总是包含端口,即使它是 80)
  • Request.ApplicationPath

难道没有一个简单的属性吗?

Isn't there a simple property for that?

推荐答案

这应该有效:

Request.Url.GetLeftPart(UriPartial.Authority) + Request.ApplicationPath

请注意,这可能会或可能不会以尾随的/"结尾,具体取决于您的应用是托管在网站的根目录中还是在子目录中.

Note that this may or may not end with a trailing "/" depending on if your app is hosted in the root of the site or in a sub directory.

这篇关于在 ASP.NET 中,获取请求的基本 URl 的最快方法是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆