Bub*_*nga 3 c# dependency-injection nullreferenceexception blazor
我尝试按照此处的建议从 URL 获取查询字符串,但收到 NullReferenceException。我的代码和链接帖子中的代码之间的唯一区别是我的代码是静态的,我不知道这会如何导致错误。
public static class Extensions
{
//Other helper methods
[Inject]
public static NavigationManager MyNavigationManager { get; set; }
public static string GetQueryParm(string parmName)
{
//Null Reference Exception is called on the line below
var uriBuilder = new UriBuilder(MyNavigationManager.Uri);
var q = System.Web.HttpUtility.ParseQueryString(uriBuilder.Query);
return q[parmName] ?? "";
}
}
Run Code Online (Sandbox Code Playgroud)
我这样调用这个方法:
else if (date == null | string.IsNullOrWhiteSpace(Extensions.GetQueryParm("d")))
{
date = DateTime.Today.ToString("yyyy-MM-dd");
}
Run Code Online (Sandbox Code Playgroud)
你不能@inject或[Inject]进入静态类。MyNavigationManager 属性永远不会被分配。
因此,忘记将其作为扩展方法并将其注入到您的 blazor 页面中。
| 归档时间: |
|
| 查看次数: |
981 次 |
| 最近记录: |