在我的MVC控制器中,我会重载OnActionExecuting方法来检查安全权限的可访问性。在Blazor serverside中路由到没有控制器的razor页面时,有没有类似的行为?
public override void OnActionExecuting(ActionExecutingContext context)
{
if (!context.IsAuthorized(out string message))
context.Result = StatusCode((int)HttpStatusCode.Unauthorized,
message);
base.OnActionExecuting(context);
}发布于 2019-09-30 09:25:05
虽然我不确定这是否是解决这个问题的最好方法(我很想知道是不是),但我已经找到了一个解决方法,希望它也能帮助其他人。
我截取Router的Found方法,如下所示:
@inject IUserContext User
<Router AppAssembly="typeof(Program).Assembly">
<Found Context="routeData">
@if (routeData.IsAuthorizedFor(User, out string message))
{
<RouteView RouteData="@routeData" DefaultLayout="typeof(MainLayout)" />
}
else
{
<LayoutView Layout="typeof(MainLayout)">
<p>@message</p>
</LayoutView>
}
</Found>
<NotFound>
<LayoutView Layout="typeof(MainLayout)">
<p>Sorry, there's nothing at this address.</p>
</LayoutView>
</NotFound>
</Router>我不认为这是最佳的原因是因为这似乎不是应该放在剃刀文件中的代码类型。Router方法也不是虚拟的,因为我确实考虑创建我自己的路由器类型,继承自那个路由器类型。
发布于 2021-02-03 07:47:54
这对我在Blazor .NET 5中的基类(即.cs文件,而不是.razor文件)有效。我没有从.razor文件中尝试)。
using Microsoft.AspNetCore.Components;
namespace YourProjectName.Pages
{
public class YourRazorFileName: ComponentBase
{
protected override void OnInitialized()
{
base.OnInitialized();
//Insert things you want to run on initialize here
}
}
}其他类似的方法: OnAfterRender,OnParametersSet。请参阅https://docs.microsoft.com/en-us/dotnet/api/microsoft.aspnetcore.components.componentbase.onafterrender?view=aspnetcore-5.0
https://stackoverflow.com/questions/58159956
复制相似问题