如何在ASP.NET MVC中获取Web应用程序的基本URL?

本文翻译自:How can I get my webapp's base URL in ASP.NET MVC?

How can I quickly determine what the root URL is for my ASP.NET MVC application? 如何快速确定ASP.NET MVC应用程序的根URL是什么? Ie, 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. 即,如果将IIS设置为在http://example.com/foo/bar上为我的应用程序提供服务,那么我希望能够以一种可靠的方式获取该URL,而无需涉及从URL获取当前URL。请求并以某种易碎的方式将其切碎,如果我重新选择操作路线,则会中断。

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. 我需要基本URL的原因是,此Web应用程序调用了另一个需要回叫方Web应用程序的根目录的应用程序。


#1楼

参考:https://stackoom.com/question/5p4W/如何在ASP-NET-MVC中获取Web应用程序的基本URL


#2楼

In Code: 在代码中:

Url.Content("~/");

MVC3 Razor Syntax: MVC3 Razor语法:

@Url.Content("~/")

#3楼

This works fine for me (also with a load balancer): 这对我来说很好(也适用于负载均衡器):

@{
    var urlHelper = new UrlHelper(Html.ViewContext.RequestContext);
    var baseurl = urlHelper.Content(“~”);
}

<script>
    var base_url = "@baseurl";
</script>

Especially if you are using non-standard port numbers, using Request.Url.Authority appears like a good lead at first, but fails in a LB environment. 尤其是如果您使用的是非标准端口号,那么使用Request.Url.Authority乍一看似乎很合适,但在LB环境中失败。


#4楼

以下代码段在MVC4中对我来说效果很好,不需要HttpContext可用:

System.Web.HttpRuntime.AppDomainAppVirtualPath

#5楼

So none of the ones listed here worked for me, but using a few of the answers, I got something working: 因此,这里列出的所有方法都不适合我,但是使用一些答案,我得到了一些有用的方法:

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;
}

Update for ASP.NET Core / MVC 6: ASP.NET Core / MVC 6的更新:

ASP.NET Core makes this process a bit more painful, especially if you are deep in your code. ASP.NET Core使此过程更加痛苦,特别是如果您深入了解代码。 You have 2 options to get at the HttpContext 您有2个选项可以使用HttpContext

1) Pass it in from your controller : 1)从您的controller

var model = new MyClass(HttpContext);

then in model : 然后在model

private HttpContext currentContext;

public MyClass(HttpContext currentContext)
{
    this.currentContext = currentContext;
}

2) Perhaps the cleaner way is to inject it into your class, which starts with registering the types in your Startup: 2)也许更干净的方法是将其注入您的类中,首先在Startup:注册类型Startup:

public void ConfigureServices(IServiceCollection services)
{
    // Add framework services.
    services.AddMvc();

    services.AddTransient<MyClass, MyClass>();
    services.TryAddSingleton<IHttpContextAccessor, HttpContextAccessor>();
}

then have it injected for you like this: 然后像这样为您注入它:

private HttpContext currentContext;

public MyClass(IHttpContextAccessor httpContextAccessor)
{
    currentContext = httpContextAccessor.HttpContext;
}

in either case, here is the updated for .NET Core GetBaseUrl() : 无论哪种情况,这都是.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}";
}

#6楼

You could have a static method that looks at HttpContext.Current and decides which URL to use (development or live server) depending on the host ID. 您可以有一个静态方法来查看HttpContext.Current并根据主机ID决定使用哪个URL(开发或实时服务器)。 HttpContext might even offer some easier way to do it, but this is the first option I found and it works fine. HttpContext甚至可以提供一些更简单的方法来完成此操作,但这是我发现的第一个选项,并且效果很好。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值