我正在构建一个使用webview的asp.net maui应用程序。我试图在用户浏览网站时捕获Request对象。
我的第一个问题是,这是否可能?
我已经尝试了几乎每一个单一的解决方案,我发现。任何帮助,这将是非常感谢。
public partial class App : Application
{
public App()
{
InitializeComponent();
var services = new ServiceCollection();
services.AddSingleton<IHttpContextAccessor, HttpContextAccessor>();
ServiceProvider = services.BuildServiceProvider();
}
public static IServiceProvider ServiceProvider { get; private set; }
}
字符串
在我的个人资料页面,我需要请求查询字符串
using Microsoft.AspNetCore.Http;
private readonly IHttpContextAccessor _httpContextAccessor;
public Profile(ProfileViewModel vm, IHttpContextAccessor httpContextAccessor) {
InitializeComponent();
_httpContextAccessor = httpContextAccessor;
BindingContext = vm;
}
private void GetQueryString() {
var context = _httpContextAccessor.HttpContext;
var queryString = context.Request.QueryString.ToString();
}
型
这引发了以下错误:无法解析类型“Microsoft.AspNetCore.Http.IHttpContextAccessor”的服务
Profile.xaml page <ContentPage.<WebView.Behaviors><local:OpenInBrowser PropertyChanging="OpenInBrowser_PropertyChanging" /></WebView.Behaviors>
我还尝试了这个OpenInBrowser_PropertyChanging处理程序,唯一会触发的方法是OnAttached,此方法仅在应用程序启动时触发一次
public class OpenInBrowser : Behavior<WebView>
{
protected override void OnAttachedTo(WebView webview) {
base.OnAttachedTo(webview);
webview.Navigating += OnNavigating;
}
protected override void OnDetachingFrom(WebView webview) {
base.OnDetachingFrom(webview);
webview.Navigating -= OnNavigating;
}
private void OnNavigating(object sender, WebNavigatingEventArgs e) {
if(e.Url.StartsWith("http")) {
//Device.OpenUri(new Uri(e.Url));
e.Cancel = true;
}
}
}
型
1条答案
按热度按时间ufj5ltwl1#
根据您支持的代码,您应该知道不建议在Blazor中使用IHttpContextAccessor/HttpContext。
如果你想在Blazor中查询参数,你可以使用NavigationManager的
Navigation.GetUriWithQueryParameter("{NAME}", {VALUE})
,{NAME}
占位符指定查询参数名称,{VALUE}
占位符指定值作为支持的类型。有关详细信息,您可以参考查询字符串。