asp.net-core-mvc – 如何从任何ASP .Net Core类访问当前的绝对Uri?

前端之家收集整理的这篇文章主要介绍了asp.net-core-mvc – 如何从任何ASP .Net Core类访问当前的绝对Uri?前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
我试图弄清楚如何从.Net Core 1.1中的用户类访问当前的绝对Uri – 即当前正在呈现的视图的绝对URL

我发现了这个链接,但它似乎已经过时并在错误后抛出错误Getting absolute URLs using ASP.NET Core MVC 6

在我的Startup.cs中,我在ConfigureServices下:

services.AddSingleton<IHttpContextAccessor,HttpContextAccessor>();

在我的Startup.cs中我有配置:

IHttpContextAccessor httpContextAccessor = app.ApplicationServices.GetrequiredService<IHttpContextAccessor>();
Extensions.Context.Configure(httpContextAccessor);

我有以下课程:

using Microsoft.AspNetCore.Http;
using System;

namespace Framework.Extensions
{
    public static class Context
    {
        private static IHttpContextAccessor HttpContextAccessor;
        public static void Configure(IHttpContextAccessor httpContextAccessor)
        {
            HttpContextAccessor = httpContextAccessor;
        }

        private static HttpContext GetCurrentContext()
        {
            return HttpContextAccessor.HttpContext;
        }
        public static HttpContext Current = GetCurrentContext();

        private static Uri GetAbsoluteUri()
        {
            UriBuilder uriBuilder = new UriBuilder();
            uriBuilder.Scheme = GetCurrentContext().Request.Scheme;
            uriBuilder.Host = GetCurrentContext().Request.Host.ToString();
            uriBuilder.Path = GetCurrentContext().Request.Path.ToString();
            uriBuilder.Query = GetCurrentContext().Request.QueryString.ToString();
            return uriBuilder.Uri;
        }
        public static Uri AbsoluteUri = GetAbsoluteUri();
        public static string Url = GetAbsoluteUri().ToString();
        public static string AbsolutePath = GetAbsoluteUri().AbsolutePath;
    }
}

我得到以下异常:

System.TypeInitializationException was unhandled by user code
HResult=-2146233036 Message=The type initializer for
‘Framework.Extensions.Context’ threw an exception.
TypeName=Framework.Extensions.Context InnerException:
HResult=-2147467261
Message=Object reference not set to an instance of an object.
Source=www
StackTrace:
at Framework.Extensions.Context.GetCurrentContext() in E:\Websites\Stage\www\Extensions\Context.cs:line 16
at Framework.Extensions.Context..cctor() in E:\Websites\Stage\www\Extensions\Context.cs:line 18
InnerException:

解决方法

您希望在启动时“配置或注入”IHttpContextAccessor,以便稍后在请求的上下文中使用帮助程序时,可以使用它来访问当前的HttpContext对象.

您无法将上下文存储在静态字段中,因为该上下文仅在提供特定请求时才有意义.通常,您将把访问器保留在静态字段中,并在每次调用助手时使用它.

>更糟糕的是,您使用带有初始化程序的静态字段,这些字段在第一次使用该类时执行.这意味着它们在您调用Configure方法之前执行,因此将不会配置IHttpContextAccessor,您将获得这些空引用.

写下你想要的东西很简单:

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

    private static Uri GetAbsoluteUri()
    {
        var request = HttpContextAccessor.HttpContext.Request;
        UriBuilder uriBuilder = new UriBuilder();
        uriBuilder.Scheme = request.Scheme;
        uriBuilder.Host = request.Host.Host;
        uriBuilder.Path = request.Path.ToString();
        uriBuilder.Query = request.QueryString.ToString();
        return uriBuilder.Uri;
    }

    // Similar methods for Url/AbsolutePath which internally call GetAbsoluteUri
    public static string GetAbsoluteUrl() { }
    public static string GetAbsolutePath() { }
}

还有一件事要记住:

>在最初的问题中,帮助程序是作为静态类创建的,因为它们是作为扩展方法创建的.如果您不使用扩展方法,则不必强制使用静态类.

原文链接:https://www.f2er.com/netcore/246930.html

猜你在找的.NET Core相关文章