【问题标题】:Unable to resolve service for type 'System.Int32' while attempting to activate尝试激活时无法解析类型“System.Int32”的服务
【发布时间】:2019-03-12 05:36:00
【问题描述】:

在 ASP.NET Core 2.2 Razor 页面网站中,我尝试从银行账户列表导航到单个银行账户的脚手架编辑页面。编辑页面打不开,显示错误信息:

System.InvalidOperationException:无法解析服务类型 尝试激活时的“System.Int32” 'ForexWeb.Pages.BankAccountModel'。在 Microsoft.Extensions.DependencyInjection.ActivatorUtilities.GetService(IServiceProvider sp, Type type, Type requiredBy, Boolean isDefaultParameterRequired)

银行账户页面 (BankAccounts.cshtml) 是

@page 
@model ForexWeb.Pages.BankAccountsModel

@{
    ViewData["Title"] = "BankAccounts";
    Layout = "~/Pages/Shared/_Layout.cshtml";
}

<h1>Bank Accounts</h1>

<table>
    @foreach (var account in Model.BankAccounts)
    {
        <tr>
            <td>@account.AccountNumber</td>
            <td>
                <a asp-page="Edit" asp-route-id="@account.Id">More</a>
                @*<a asp-action="Account/Edit" asp-route-id="@account.Id">Edit</a>*@
            </td>
        </tr>
    }
</table>

编辑页面(Edit.cshtml)以

开头
@page "{id}"
@model ForexWeb.Pages.BankAccountModel

@{
    ViewData["Title"] = "BankAccount";
    Layout = "~/Pages/Shared/_Layout.cshtml";
}

<h1>BankAccount</h1>

路由是在 Startup.cs 中设置的

public void Configure(IApplicationBuilder app, IHostingEnvironment env)
{
    if (env.IsDevelopment())
    {
        app.UseDeveloperExceptionPage();
        app.UseDatabaseErrorPage();
    }
    else
    {
        app.UseExceptionHandler("/Error");
        app.UseHsts();
    }

    app.UseHttpsRedirection();
    app.UseStaticFiles();
    app.UseCookiePolicy();

    app.UseAuthentication();

    app.UseMvc(ConfigureRoutes);
}

private void ConfigureRoutes(IRouteBuilder routeBuilder)
{
    routeBuilder.MapRoute("Default", "{controller=Home}/action=Index/{id?}");
}

BankAccountModel 类

public class BankAccountModel : PageModel
{
    private readonly int _id;
    private readonly IForexRepository _repository;

    public BankAccountModel(int id, IForexRepository repository)
    {
        _id = id;
        _repository = repository;
    }

    [BindProperty]
    public ForexRepository.Entities.BankAccount BankAccount { get; set; }

    public async Task<IActionResult> OnGetAsync(int? id)
    {
        if (id == null)
        {
            return NotFound();
        }

        BankAccount =  _repository.GetBankAccount((int)id);


        if (BankAccount == null)
        {
            return NotFound();
        }
        ViewData["BankId"] = new SelectList(_repository.GetBanks(), "Id", "Id");
        return Page();
    }

    public IActionResult OnGet()
    {
        var bankAccount = _repository.GetBankAccount(_id);
        BankAccount = bankAccount;
        return Page();
    }

    public async Task<IActionResult> OnPostAsync()
    {
        if (!ModelState.IsValid)
        {
            return Page();
        }

        _repository.AddBankAccount(BankAccount.BankId,BankAccount);

        try
        {
            _repository.Save();
        }
        catch (DbUpdateConcurrencyException)
        {
            if (!BankAccountExists(BankAccount.Id))
            {
                return NotFound();
            }
            else
            {
                throw;
            }
        }

        return RedirectToPage("./Index");
    }

    private bool BankAccountExists(int id)
    {
        return _repository.GetBankAccount(id) != null;
    }
}

可能是什么问题?

【问题讨论】:

    标签: c# url-routing razor-pages


    【解决方案1】:

    BankAccountModel 由 DI 容器创建。在创建页面模型时,容器正在解析所有构造函数参数,以便注入它们并创建BankAccountModel 的实例。当容器尝试解析显然未注册的int 类型的服务时,将引发异常。所以你需要从构造函数中移除int参数

    public BankAccountModel(IForexRepository repository)
    {
        _repository = repository;
    }
    

    【讨论】:

      猜你喜欢
      • 2018-04-06
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-03-23
      • 2018-09-24
      • 1970-01-01
      • 2019-02-04
      相关资源
      最近更新 更多