获得绝对URL的使用ASP.NET MVC 6 [英] Getting Absolute URL's using ASP.NET MVC 6

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

问题描述

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

In MVC 5, I had the following extension methods to generate absolute URL's, 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);
    }
}

什么相当于在MVC 6?

What would the equivalent be in MVC 6?

  • UrlHelper.RequestContext 已不存在。
  • 您不能弄个的HttpContext 的,因为不再是一个静态的 HttpContext.Current 属性。
  • 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.AspNet.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());

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

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