Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/asp.net-core/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 在ASP.NET Core 3.0中,Url.Action()返回null_C#_Asp.net Core_Asp.net Core Mvc_Visual Studio 2019 - Fatal编程技术网

C# 在ASP.NET Core 3.0中,Url.Action()返回null

C# 在ASP.NET Core 3.0中,Url.Action()返回null,c#,asp.net-core,asp.net-core-mvc,visual-studio-2019,C#,Asp.net Core,Asp.net Core Mvc,Visual Studio 2019,迁移到ASP.NET Core 3.0后。Action()返回有效操作、控制器的null。这在ASP.NET core 2.2中起作用 如中所述,操作名称不包含异步 配置方法 app.UseEndpoints(endpoints => { endpoints.MapControllerRoute( name: "default", pattern:

迁移到ASP.NET Core 3.0后。Action()返回有效操作、控制器的null。这在ASP.NET core 2.2中起作用

如中所述,操作名称不包含异步

配置方法

   app.UseEndpoints(endpoints =>
            {
                endpoints.MapControllerRoute(
                    name: "default",
                    pattern: "{controller=Home}/{action=Index}/{id?}");
            });
        services.AddControllersWithViews()
            .AddControllersAsServices()
            .AddSessionStateTempDataProvider()
            .AddMvcLocalization();
配置服务方法

   app.UseEndpoints(endpoints =>
            {
                endpoints.MapControllerRoute(
                    name: "default",
                    pattern: "{controller=Home}/{action=Index}/{id?}");
            });
        services.AddControllersWithViews()
            .AddControllersAsServices()
            .AddSessionStateTempDataProvider()
            .AddMvcLocalization();
例如

    // url is null
    string url = Url.Action("DeleteAccount", "Manage", null);

我遗漏了什么?

我遇到了和你一样的问题。 我认为您没有在configureServices函数中添加NewtonSoftJSON。 尝试添加它

  <PackageReference Include="Microsoft.AspNetCore.Mvc.NewtonsoftJson" Version="3.0.0" /> 

我遇到了和你一样的问题。 我认为您没有在configureServices函数中添加NewtonSoftJSON。 尝试添加它

  <PackageReference Include="Microsoft.AspNetCore.Mvc.NewtonsoftJson" Version="3.0.0" /> 

演示您是如何使用它的?我在路由方面遇到了一些问题,这似乎是因为我错过了将“services.AddMvc”交换到“services.AddRazorPage”的过程。我会再次检查您是否捕获了所有需要从启动时删除的项。@mvermef我正在尝试在单元测试中验证url。为此,我使用了DI中的LinkGenerator,并创建了一个从UrlHelperBase继承的自定义模拟端点UrlHelperMock。然后将其设置为Controller.Url属性。看起来LinkGenerator.GetPathByAction(actionContext.Action,actionContext.Controller,actionContext.Values)返回null…我也看到了这个问题。您找到解决方案了吗?我在使用
IUrlHelper.Link(RouteName)
进行属性路由时遇到了同样的问题。在属性上正确设置路由名称,并使用
MapControllers()配置端点
。告诉我们您是如何使用它的?我在路由方面遇到了一些问题,这似乎是因为我错过了将“services.AddMvc”交换到“services.AddRazorPage”的过程。我会再次检查您是否捕获了所有需要从启动中删除的项。@mvermef我正在尝试在单元测试中验证url。为此,我使用了DI中的LinkGenerator,并创建了一个从UrlHelperBase继承的自定义模拟端点UrlHelperMock。然后将其设置为Controller.Url属性。看起来LinkGenerator.GetPathByAction(actionContext.Action,actionContext.Controller,actionContext.Values)返回null…我也看到了这个问题。您找到解决方案了吗?我在使用
IUrlHelper.Link(RouteName)
进行属性路由时遇到了同样的问题。在属性上正确设置路由名称,并使用
MapControllers()
配置端点。