我怎样才能在ASP.NET MVC Web应用我的基本网址是什么? [英] How can I get my webapp's base URL in ASP.NET MVC?
问题描述
如何快速确定根本网址是什么,我的ASP.NET MVC应用程序?也就是说,如果IIS设置为我的在 http://example.com/foo/bar 的应用程序,那么我想以能够得到该URL中不涉及从请求获取当前的URL和斩波它在一些打破如果我重新路由我的动作脆弱方式的可靠方法。
How can I quickly determine what the root URL is for my ASP.NET MVC application? I.e., if IIS is set to serve my application at http://example.com/foo/bar, then I'd like to be able to get that URL in a reliable way that doesn't involve getting the current URL from the request and chopping it up in some fragile way that breaks if I re-route my action.
这是我所需要的基本URL的原因是此Web应用程序调用另外一个需要根给调用者的Web应用程序回调的目的。
The reason that I need the base URL is that this web application calls another one that needs the root to the caller web application for callback purposes.
推荐答案
假设你有一个请求对象可用,你可以使用:
Assuming you have a Request object available, you can use:
string.Format("{0}://{1}{2}", Request.Url.Scheme, Request.Url.Authority, Url.Content("~"));
如果它是不可用,您可以通过上下文得到它:
If it's not available, you can get to it via the context:
var request = HttpContext.Current.Request
这篇关于我怎样才能在ASP.NET MVC Web应用我的基本网址是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!