Check the docs before asking a question: How to use extended field query in EF query?, [Filter User list by new extra property #784](https://support.abp.io/QA/Questions/784/Filter-User-list-by-new-extra-property), How to add custom property to the user entity,Module Entity Extensions | Documentation Center | ABP.IO ...
- ABP Framework version: v8.0.2
- UI Type: Blazor WASM / Blazor Server
- Database System: EF Core (PostgreSQL)
Hi Support Teams,
I have two questions:
- Is there a way to use ScreenUrl within ExtraProperties instead of creating an additional ScreenUrl column and using EF.Property(u, "ScreenUrl") to write the query statement?
2. How can I add `context.GetHttpContext().Request.Headers["screen-url"]` to the `ScreenUrl` column in `EntityChanges`? Currently, when I run it, it only adds it to the `ScreenUrl` in `ExtraProperties` but doesn't save it to the newly added `ScreenUrl` column.
public class ExtendedAuditLogContributor : AuditLogContributor
{
public override void PreContribute(AuditLogContributionContext context)
{
context.AuditInfo.SetProperty(
"ScreenUrl",
context.GetHttpContext().Request.Headers["screen-url"]
);
}
public override void PostContribute(AuditLogContributionContext context)
{
var currentUser = context.ServiceProvider.GetRequiredService<ICurrentUser>();
foreach (var change in context.AuditInfo.EntityChanges)
{
change.SetProperty(
"ScreenUrl",
context.GetHttpContext().Request.Headers["screen-url"]);
change.SetProperty(
"UserId", currentUser.Id);
}
}
}
21 Answer(s)
-
0
Hi,
Is there a way to use ScreenUrl within ExtraProperties instead of creating an additional ScreenUrl column and using EF.Property(u, "ScreenUrl") to write the query statement?
Unfortunately no, It will be calculated on the client side instead of the server client, which will affect query performance: https://learn.microsoft.com/en-us/ef/core/querying/client-eval
How can I add
context.GetHttpContext().Request.Headers["screen-url"]
to theScreenUrl
column inEntityChanges
? Currently, when I run it, it only adds it to theScreenUrl
inExtraProperties
but doesn't save it to the newly addedScreenUrl
column.Could you share a simple minimal project to reproduce the problem? I will check it, thanks. my email is shiwei.liang@volosoft.com
-
0
Hi liangshiwei,
I have sent you an email, please check and assist me. Thanks.
-
0
-
0
Sorry, I have sent you a new email. Please check it again.
-
0
-
0
-
0
Hi,
I made some changes to the project and it works for me.
I have shared the project with you via email, you can check it
Steps
- Run
dotnet ef database update
to create database. (for authserver and httpapihost) - Run the AuthServer, HttpApi.Host and Blazor project
- Login and navigate to
common
page - Click the
Create test
button
- Run
-
0
It worked. Thank you very much.
But I'm still facing an issue. Currently, in Blazor WebAssembly, I have code in
HttpApi.Client
.and I use
context.GetHttpContext().Request.Headers["screen-url"].ToString();
to get the URL.However, using this approach won't work in Blazor Server. Is there any way to retrieve the URL when using Blazor Server?
-
0
Is the Blazor server a tiered solution?
-
0
Is the Blazor server a tiered solution?
Yes, that's correct.
-
0
I think it should be working.
How I reproduce the problem? I will check it.
-
0
-
0
You can try:
public class MyHubFilter : IHubFilter { public ValueTask<object?> InvokeMethodAsync(HubInvocationContext invocationContext, Func<HubInvocationContext, ValueTask<object?>> next) { var httpContext = invocationContext.Context.GetHttpContext(); if (httpContext != null) { var navigationManager = invocationContext.ServiceProvider.GetRequiredService<NavigationManager>(); httpContext.Request.Headers["screen-url"] = navigationManager.Uri; } return next(invocationContext); } } public override void PreConfigureServices(ServiceConfigurationContext context) { Configure<HubOptions>(options => { options.AddFilter<MyHubFilter>(); }); }
-
0
-
0
Please add to the Blazor server instead of HttpAPI project
-
0
I'm still unable to retrieve the Screen-url when using Blazor Server. :(
-
0
Can you share a project?
-
0
Can you share a project?
Sure, I have created a new project, and I have sent it to your email.
-
0
You did not configure the
AuditMessageHandler
public override void PreConfigureServices(ServiceConfigurationContext context) { PreConfigure<AbpHttpClientBuilderOptions>(options => { options.ProxyClientBuildActions.Add((remoteServiceName, clientBuilder) => { clientBuilder.AddHttpMessageHandler<AuditMessageHandler>(); }); }); }
-
0
-
0
Hi,
Yes you need to make some changes
Idea from: https://stackoverflow.com/questions/77129117/blazor-navigationmanager-not-initialized-error
Add
AppInfo
class to Blazor server project.public class AppInfo : ISingletonDependency { public string? CurrentUrl { get; private set; } public void UpdateAppUrl(string url) { this.CurrentUrl = url; } }
Move
AuditMessageHandler
class to Blazor Server projectpublic class AuditMessageHandler : DelegatingHandler, ITransientDependency { private readonly AppInfo _appInfo; public AuditMessageHandler(AppInfo appInfo) { _appInfo = appInfo; } protected override Task<HttpResponseMessage> SendAsync(HttpRequestMessage request, CancellationToken cancellationToken) { request.Headers.Add("screen-url", _appInfo.CurrentUrl); return base.SendAsync(request, cancellationToken); } }
Add
AppInfoProvider.razor
to Blazor server project.@code { [Inject] private NavigationManager NavigationManager { get; set; } = default!; [Inject] private AppInfo AppInfo { get; set; } = default!; protected override void OnInitialized() { NavigationManager.LocationChanged += (sender, args) => { this.AppInfo.UpdateAppUrl(this.NavigationManager.Uri); }; } } ------------------- Configure<AbpLayoutHookOptions>(options => { options.Add(LayoutHooks.Body.First, typeof(AppInfoProvider),StandardLayouts.Application); });