问题描述
如何快速确定我的 ASP.NET MVC 应用程序的根 URL 是什么?即,如果 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 应用程序调用另一个需要根 URL 到调用方 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.
推荐答案
假设你有一个可用的 Request 对象,你可以使用:
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 中获取我的 webapp 的基本 URL?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!