获取使用ASP.NET MVC核心6绝对URL [英] Getting absolute URLs using ASP.NET Core MVC 6

查看:477
本文介绍了获取使用ASP.NET MVC核心6绝对URL的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在MVC 5,我有以下的扩展方法来生成绝对URL,而不是相对的:

In MVC 5, I had the following extension methods to generate absolute URLs, instead of relative ones:

public static class UrlHelperExtensions
{
    public static string AbsoluteAction(
        this UrlHelper url,
        string actionName, 
        string controllerName, 
        object routeValues = null)
    {
        string scheme = url.RequestContext.HttpContext.Request.Url.Scheme;
        return url.Action(actionName, controllerName, routeValues, scheme);
    }

    public static string AbsoluteContent(
        this UrlHelper url,
        string contentPath)
    {
        return new Uri(url.RequestContext.HttpContext.Request.Url, url.Content(contentPath)).ToString();
    }

    public static string AbsoluteRouteUrl(
        this UrlHelper url,
        string routeName,
        object routeValues = null)
    {
        string scheme = url.RequestContext.HttpContext.Request.Url.Scheme;
        return url.RouteUrl(routeName, routeValues, scheme);
    }
}

就相当于是什么在ASP.NET MVC的核心6?

What would the equivalent be in ASP.NET Core MVC 6?


  • UrlHelper.RequestContext 不再存在。

  • ,因为不再是一个静态 HttpContext.Current 属性你不能得到的的HttpContext 搁置。

  • UrlHelper.RequestContext no longer exists.
  • You can't get hold of the HttpContext as there is no longer a static HttpContext.Current property.

据我所看到的,你现在要求在的HttpContext 的Htt prequest 反对在也被传递。我对吗?是否有某种方式来获得当前请求的暂停?

As far as I can see, you would now require the HttpContext or HttpRequest objects to be passed in also. Am I right? Is there some way to get hold of the current request?

我是即使是在正确的轨道,应该域现在是一个环境变量,这是简单的追加到相对URL?这会是一个更好的办法?

Am I even on the right track, should the domain now be an environment variable, which is simple appended to the relative URL? Would this be a better approach?

推荐答案

您可以修改您的扩展类使用<一个href=\"https://github.com/aspnet/Hosting/blob/dev/src/Microsoft.AspNet.Hosting/HttpContextAccessor.cs\"相对=nofollow> IHttpContextAccessor ​​ 接口获取的<一个href=\"https://github.com/aspnet/HttpAbstractions/blob/dev/src/Microsoft.AspNet.Http.Abstractions/HttpContext.cs\"相对=nofollow> 的HttpContext 。一旦你的环境,那么你可以得到的<一个href=\"https://github.com/aspnet/HttpAbstractions/blob/dev/src/Microsoft.AspNet.Http.Abstractions/Htt$p$pquest.cs\"相对=nofollow> 的Htt prequest 例如从 HttpContext.Request 并使用其属性计划主机协议等为:

You could modify your extension class to use the IHttpContextAccessor interface to get the HttpContext. Once you have the context, then you can get the HttpRequest instance from HttpContext.Request and use its properties Scheme, Host, Protocol etc as in:

string scheme = HttpContextAccessor.HttpContext.Request.Scheme;

例如,你可以要求你的类与HttpContextAccessor进行配置:

For example, you could require your class to be configured with an HttpContextAccessor:

public static class UrlHelperExtensions
{        
    private static IHttpContextAccessor HttpContextAccessor;
    public static void Configure(IHttpContextAccessor httpContextAccessor)
    {           
        HttpContextAccessor = httpContextAccessor;  
    }

    public static string AbsoluteAction(
        this IUrlHelper url,
        string actionName, 
        string controllerName, 
        object routeValues = null)
    {
        string scheme = HttpContextAccessor.HttpContext.Request.Scheme;
        return url.Action(actionName, controllerName, routeValues, scheme);
    }

    ....
}

这是一件好事,你可以在你的启动类(Startup.cs文件)做的:

Which is something you can do on your Startup class (Startup.cs file):

public void Configure(IApplicationBuilder app)
{
    ...

    var httpContextAccessor = app.ApplicationServices.GetRequiredService<IHttpContextAccessor>();
    UrlHelperExtensions.Configure(httpContextAccessor);

    ...
}

您也许可以想出了让不同的方式 IHttpContextAccessor ​​在扩展类,但如果你想保持你的方法在你需要结束的扩展方法注入 IHttpContextAccessor ​​到您的静态类。 (否则你将需要 IHttpContext 为每个调用的参数)

You could probably come up with different ways of getting the IHttpContextAccessor in your extension class, but if you want to keep your methods as extension methods in the end you will need to inject the IHttpContextAccessor into your static class. (Otherwise you will need the IHttpContext as an argument on each call)

修改

由于指向的热汗·赛义德,它能够更好地扩展方法添加到接口而不是 IUrlHelper UrlHelper

As pointed by Rehan Saeed, its better to add the extension methods to the interface IUrlHelper instead of UrlHelper

刚开当前请求的绝对URI

如果你只是想获得当前请求的绝对URI,则可以使用扩展方法 GetDisplayUrl GetEn codedUrl 从<一个href=\"https://github.com/aspnet/HttpAbstractions/blob/dev/src/Microsoft.AspNetCore.Http.Extensions/UriHelper.cs\"相对=nofollow> UriHelper 类。

If you just want to get the absolute uri of the current request, you can use the extension methods GetDisplayUrl or GetEncodedUrl from the UriHelper class.

GetDisplayUrl 。返回请求URL的组合组件在完全非转义形式(除了查询字符串)只适合
  进行显示。这种格式不应该在HTTP标头或其他使用
  HTTP操作。

GetDisplayUrl. Returns the combined components of the request URL in a fully un-escaped form (except for the QueryString) suitable only for display. This format should not be used in HTTP headers or other HTTP operations.

GetEn codedUrl 。返回请求URL的联合组件适用于HTTP标头和其他用途完全逃出
  HTTP操作。

GetEncodedUrl. Returns the combined components of the request URL in a fully escaped form suitable for use in HTTP headers and other HTTP operations.

为了使用它们:


  • 包含命名空间 Microsoft.AspNet.Http.Extensions

  • 获得的HttpContext 实例。它在某些类(比如剃须刀意见)已经上市,但在​​其他你可能需要注入的 IHttpContextAccessor ​​如上所述。

  • 然后,只需使用它们作为 this.Context.Request.GetDisplayUrl()

  • Include the namespace Microsoft.AspNet.Http.Extensions.
  • Get the HttpContext instance. It is already available in some classes (like razor views), but in others you might need to inject an IHttpContextAccessor as explained above.
  • Then just use them as in this.Context.Request.GetDisplayUrl()

这些方法另一种方法是手动各具特色自己使用的 HttpContext.Request 对象中的值的绝对URI(类似于在<一个href=\"https://github.com/aspnet/Mvc/blob/046cb976b3e899052a95387b72ea4bee6987bff0/src/Microsoft.AspNet.Mvc.Core/RequireHttpsAttribute.cs\"相对=nofollow> RequireHttpsAttribute 一样):

An alternative to those methods would be manually crafting yourself the absolute uri using the values in the HttpContext.Request object (Similar to what the RequireHttpsAttribute does):

var absoluteUri = string.Concat(
                        request.Scheme,
                        "://",
                        request.Host.ToUriComponent(),
                        request.PathBase.ToUriComponent(),
                        request.Path.ToUriComponent(),
                        request.QueryString.ToUriComponent());

这篇关于获取使用ASP.NET MVC核心6绝对URL的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆