C# 获取应用程序 url

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/18065955/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-10 11:12:01  来源:igfitidea点击:

C# Get application url

c#asp.net

提问by socketman

I am trying to get the application url in C#. I have tried using:

我正在尝试在 C# 中获取应用程序 url。我试过使用:

HttpContext.Current.Request.ApplicationPath 

But that only returns me "/". I am trying to get the full url of application's base url, which may or may not simply be "something.com". In some cases, depending on the environment, it is "something.com/foo/bar".

但这只会返回我“/”。我正在尝试获取应用程序基本 url 的完整 url,它可能是也可能不是简单的“something.com”。在某些情况下,根据环境,它是“something.com/foo/bar”。

How can I achieve this?

我怎样才能做到这一点?

采纳答案by Daniel A. White

Wouldn't the RawUrlbe enough for you?

岂不RawUrl是够吗?

If that gives you too much, you could try

如果那给你太多,你可以试试

VirtualPathUtility.ToAbsolute("~/");

回答by Karl Anderson

Try this:

尝试这个:

string baseUrl = Request.Url.Scheme + "://" + Request.Url.Authority + Request.ApplicationPath.TrimEnd('/');