简体   繁体   English

Url.Action() 在 ASP.NET 核心 3.0 中返回 null

[英]Url.Action() returns null in ASP.NET Core 3.0

After migrating to the ASP.NET Core 3.0 .迁移到ASP.NET Core 3.0之后。 The Url.Action() returns null for valid action, controller. Url.Action() 为有效操作返回null ,controller。 This worked in ASP.NET core 2.2这适用于 ASP.NET 核心 2.2

The action name does not contain async as explained in Microsoft Doc for migration to core 3.0操作名称包含 async,如Microsoft Doc 中用于迁移到核心 3.0的说明

Configure method配置方法

   app.UseEndpoints(endpoints =>
            {
                endpoints.MapControllerRoute(
                    name: "default",
                    pattern: "{controller=Home}/{action=Index}/{id?}");
            });

ConfigureServices method配置服务方法

        services.AddControllersWithViews()
            .AddControllersAsServices()
            .AddSessionStateTempDataProvider()
            .AddMvcLocalization();

Eg例如

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

What am i missing?我错过了什么?

I have met same question as yours.我遇到了和你一样的问题。 I think you haven't AddNewtonsoftjson in your configureServices function.我认为您的 configureServices function 中没有 AddNewtonsoftjson。 Try to add it.尝试添加它。

  <PackageReference Include="Microsoft.AspNetCore.Mvc.NewtonsoftJson" Version="3.0.0" /> 
 services.AddControllersWithViews().AddNewtonsoftJson();

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM