Isa*_*aac 14 blazor blazor-server-side
IP 地址等。通常,当用户询问如何在 Server Blazor 应用程序中执行此操作时,他们要么被告知这是不可能的,要么有时会提供使用 JSInterop 的部分解决方案。但是可以在不诉诸 JSInterop 的情况下完成吗?这是答案...
Isa*_*aac 36
该小说HttpContext
对象无法与Blazor服务器应用程序可以使用,已经长繁殖#1,它是高的时候养老金其关闭。
确实,HttpContext
当 WebSocket 连接在运行时不可用,但必须明确:当您键入 url 并按 Enter 按钮时,与服务器端 Blazor 应用程序的连接是 HTTP 连接,而不是WebSocket 连接。
因此,您的应用程序可以HttpContext
像在 Razor Pages 应用程序或 MVC 应用程序中一样访问和使用它,包括获取用户代理和 IP 地址。以下代码示例演示了如何使用HttpContext
本地获取用户代理和 IP 地址,而不使用JSInterop
应作为最后手段使用的,并将提取的值传递给App
组件。
Pages
夹中并将其命名为_Host.cshtml.cs
.public class HostModel: PageModel
{
private readonly IHttpContextAccessor _httpContextAccssor;
public HttpContextFeatureModel(IHttpContextAccessor httpContextAccssor)
{
_httpContextAccssor = httpContextAccssor;
}
public string UserAgent { get; set; }
public string IPAddress { get; set; }
// The following links may be useful for getting the IP Adress:
// https://stackoverflow.com/questions/35441521/remoteipaddress-is-always-null
// https://stackoverflow.com/questions/28664686/how-do-i-get-client-ip-address-in-asp-net-core
public void OnGet()
{
UserAgent = _httpContextAccssor.HttpContext.Request.Headers["User-Agent"];
// Note that the RemoteIpAddress property returns an IPAdrress object
// which you can query to get required information. Here, however, we pass
// its string representation
IPAddress = _httpContextAccssor.HttpContext.Connection.RemoteIpAddress.ToString();
}
}
Run Code Online (Sandbox Code Playgroud)
您可能需要以下一种或多种用途:
using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using System.Linq;
using System.Text.Encodings.Web;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Authentication;
using Microsoft.AspNetCore.Identity;
using Microsoft.AspNetCore.Identity.UI.Services;
using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.Mvc.RazorPages;
using Microsoft.Extensions.Logging;
using Microsoft.AspNetCore.Http;
Run Code Online (Sandbox Code Playgroud)
Host.cshthml
将以下行添加到您的页面(在靠近 using 和那些东西的页面顶部):@model HostModel
Run Code Online (Sandbox Code Playgroud)
App
组件中,定义两个参数属性,这些属性将获取并存储从位于 中的组件标记传递给它的用户代理和 IP 地址_Host.cshtml
。App.razor:
<p>UserAgent: @UserAgent</p>
<p>IPAddress: @IPAddress</p>
@code
{
[Parameter]
public string UserAgent { get; set; }
[Parameter]
public string IPAddress { get; set; }
}
Run Code Online (Sandbox Code Playgroud)
_Host.cshtml
更新组件标签这样的(这个方法现在已经过时了):<app>
<component type="typeof(App)" render-mode="ServerPrerendered" param-UserAgent="@Model.UserAgent" param-IPAddress="@Model.IPAddress" />
</app>
Run Code Online (Sandbox Code Playgroud)
在当前的 Blazor 服务器端应用程序中,可以这样做:
<div>
@(await Html.RenderComponentAsync<App>(RenderMode.Server, new { IPAddress = Model.IPAddress, UserAgent = Model.UserAgent }))
</div>
Run Code Online (Sandbox Code Playgroud)
services.AddHttpContextAccessor();
toStartup
的ConfigureServices
方法以启用对 HttpContext 的访问。就这些。您也可以将 添加Identity UI
到您的 Blazor 服务器应用程序,并应用上面显示的相同过程从 中提取声明主体HttpContext
,在用户通过身份验证后(这样做仅用于学习目的,因为您应该使用 AuthenticationStateProvider)。
归档时间: |
|
查看次数: |
9328 次 |
最近记录: |