如何快速确定我的ASP的根URL是什么?NET MVC应用程序?例如,如果IIS设置为在http://example.com/foo/bar上为我的应用程序服务,那么我希望能够以一种可靠的方式获得该URL,而不涉及从请求中获取当前URL,并以某种脆弱的方式将其分割,如果我重新路由我的操作,这种方式就会中断。
我需要基本URL的原因是这个web应用程序调用另一个需要根的调用者web应用程序的回调目的。
如何快速确定我的ASP的根URL是什么?NET MVC应用程序?例如,如果IIS设置为在http://example.com/foo/bar上为我的应用程序服务,那么我希望能够以一种可靠的方式获得该URL,而不涉及从请求中获取当前URL,并以某种脆弱的方式将其分割,如果我重新路由我的操作,这种方式就会中断。
我需要基本URL的原因是这个web应用程序调用另一个需要根的调用者web应用程序的回调目的。
当前回答
所以这里列出的这些方法都不适合我,但通过使用其中的一些答案,我找到了一些有用的方法:
public string GetBaseUrl()
{
var request = HttpContext.Current.Request;
var appUrl = HttpRuntime.AppDomainAppVirtualPath;
if (appUrl != "/")
appUrl = "/" + appUrl;
var baseUrl = string.Format("{0}://{1}{2}", request.Url.Scheme, request.Url.Authority, appUrl);
return baseUrl;
}
更新ASP。NET Core / MVC 6:
ASP。NET Core使这个过程更加痛苦,特别是当您深入代码时。你有两个选项来获取HttpContext
1)从控制器传入:
var model = new MyClass(HttpContext);
然后在模型中:
private HttpContext currentContext;
public MyClass(HttpContext currentContext)
{
this.currentContext = currentContext;
}
2)也许更干净的方法是将它注入到你的类中,首先在你的Startup中注册类型:
public void ConfigureServices(IServiceCollection services)
{
// Add framework services.
services.AddMvc();
services.AddTransient<MyClass, MyClass>();
services.TryAddSingleton<IHttpContextAccessor, HttpContextAccessor>();
}
然后像这样为你注射:
private HttpContext currentContext;
public MyClass(IHttpContextAccessor httpContextAccessor)
{
currentContext = httpContextAccessor.HttpContext;
}
在任何情况下,这是更新的。net Core GetBaseUrl():
public string GetBaseUrl()
{
var request = currentContext.Request;
var host = request.Host.ToUriComponent();
var pathBase = request.PathBase.ToUriComponent();
return $"{request.Scheme}://{host}{pathBase}";
}
其他回答
也许这是对这里发布的答案的扩展或修改,但我简单地使用了下面这行,它是有效的:
Request.Url.GetLeftPart(UriPartial.Authority) + Url.Content("~")
当我的路径是:http://host/iis_foldername/controller/action 然后我收到:http://host/iis_foldername/
这是一个asp.net属性到MVC的转换。这是一个很好的方法。
声明一个helper类:
namespace MyTestProject.Helpers
{
using System.Web;
public static class PathHelper
{
public static string FullyQualifiedApplicationPath(HttpRequestBase httpRequestBase)
{
string appPath = string.Empty;
if (httpRequestBase != null)
{
//Formatting the fully qualified website url/name
appPath = string.Format("{0}://{1}{2}{3}",
httpRequestBase.Url.Scheme,
httpRequestBase.Url.Host,
httpRequestBase.Url.Port == 80 ? string.Empty : ":" + httpRequestBase.Url.Port,
httpRequestBase.ApplicationPath);
}
if (!appPath.EndsWith("/"))
{
appPath += "/";
}
return appPath;
}
}
}
用法:
从控制器使用:
PathHelper.FullyQualifiedApplicationPath(ControllerContext.RequestContext.HttpContext.Request)
在视图中使用:
@using MyTestProject.Helpers
PathHelper.FullyQualifiedApplicationPath(Request)
下面的方法对我很有效
var request = HttpContext.Request;
var appUrl = System.Web.HttpRuntime.AppDomainAppVirtualPath;
if (appUrl != "/")
appUrl = "/" + appUrl + "/";
var newUrl = string.Format("{0}://{1}{2}{3}/{4}", request.Url.Scheme, request.UrlReferrer.Host, appUrl, "Controller", "Action");
将此函数添加到项目的静态类中,如实用程序类:
utility.cs内容:
public static class Utility
{
public static string GetBaseUrl()
{
var request = HttpContext.Current.Request;
var urlHelper = new UrlHelper(request.RequestContext);
var baseUrl = $"{request.Url.Scheme}://{request.Url.Authority}{urlHelper.Content("~")}";
return baseUrl;
}
}
在任何地方使用这段代码并享受它:
var baseUrl = Utility.GetBaseUrl();
网页本身:
<input type="hidden" id="basePath" value="@string.Format("{0}://{1}{2}",
HttpContext.Current.Request.Url.Scheme,
HttpContext.Current.Request.Url.Authority,
Url.Content("~"))" />
在javascript中:
function getReportFormGeneratorPath() {
var formPath = $('#reportForm').attr('action');
var newPath = $("#basePath").val() + formPath;
return newPath;
}
这适用于我的MVC项目,希望它有帮助