Blazor: 获取`ActionContext`
创始人
2024-12-21 00:01:13
0

要获取Blazor中的ActionContext,可以通过注入NavigationManager服务来实现。以下是一个包含代码示例的解决方法:

  1. 首先,确保您的Blazor应用程序已经引用了以下命名空间:
using Microsoft.AspNetCore.Components;
  1. 创建一个名为ActionContextService的服务类,用于获取ActionContext
public class ActionContextService
{
    private readonly NavigationManager _navigationManager;

    public ActionContextService(NavigationManager navigationManager)
    {
        _navigationManager = navigationManager;
    }

    public ActionContext GetActionContext()
    {
        var uri = _navigationManager.Uri;
        return _navigationManager.ToBaseRelativePath(uri);
    }
}
  1. Startup.cs文件中注册ActionContextService服务:
services.AddScoped();
  1. 现在,您可以在任何组件中注入ActionContextService并使用它来获取ActionContext。例如:
@page "/example"

@inject ActionContextService ActionContextService

ActionContext Example

Current ActionContext: @ActionContextService.GetActionContext()

在上面的示例中,ActionContextService通过依赖注入被注入到组件中,并在@ActionContextService.GetActionContext()中调用GetActionContext方法来获取当前页面的ActionContext

相关内容

热门资讯

黑科技辅助挂!德扑之星软件模拟... 黑科技辅助挂!德扑之星软件模拟器,wepoke辅助器,解密教程(有人有挂);黑科技辅助挂!德扑之星软...
黑科技辅助挂!wepoke软件... 您好,智星德州菠萝偷偷看牌功能这款游戏可以开挂的,确实是有挂的,需要了解加微【136704302】很...
黑科技辅助挂!德州辅助神器软件... 黑科技辅助挂!德州辅助神器软件,云扑克德州辅助器,wepoke教程(有挂头条);一、云扑克德州辅助器...
黑科技辅助挂!德州wpk有外挂... 您好,aapoker安卓版辅助这款游戏可以开挂的,确实是有挂的,需要了解加微【136704302】很...
黑科技辅助挂!德州免费辅助神器... 黑科技辅助挂!德州免费辅助神器app,we-poke有挂,AI教程(了解有挂);黑科技辅助挂!德州免...
黑科技辅助挂!wepoke有挂... 黑科技辅助挂!wepoke有挂,wepoker真的有挂,软件教程(有挂辅助);黑科技辅助挂!wepo...
黑科技辅助挂!德扑ai助手,a... 您好,aapoker有人用AI这款游戏可以开挂的,确实是有挂的,需要了解加微【136704302】很...
黑科技辅助挂!wpk微扑克辅助... 黑科技辅助挂!wpk微扑克辅助ai,aa扑克辅助,技巧教程(有挂猫腻)是一款可以让一直输的玩家,快速...
黑科技辅助挂!德扑ai机器人软... 您好:德扑ai机器人软件开发这款游戏可以开挂的,确实是有挂的,很多玩家在这款游戏中打牌都会发现很多用...
黑科技辅助挂!德州游戏辅助器大... 黑科技辅助挂!德州游戏辅助器大全,wpk微扑克真的有挂,总结教程(有挂工具);黑科技辅助挂!德州游戏...