AmbigeousMatchException:请求匹配多个端点 - 索引和错误

问题描述 投票:0回答:1

我收到以下错误:

AmbiguousMatchException: The request matched multiple endpoints. Matches:

BeyondTrustConsole.Controllers.BT_ChallengeResponseController.Index (BeyondTrustConsole)
BeyondTrustConsole.Controllers.BT_ChallengeResponseController.Error (BeyondTrustConsole)

我的控制器看起来像这样:

using System.Diagnostics;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc;
using BeyondTrustConsole.Models;
using BeyondTrustConsole.ViewModels;
using System.Text.RegularExpressions;
using System.Runtime.InteropServices;
using System.Security.Claims;

namespace BeyondTrustConsole.Controllers;

[Authorize(Policy = "Role_ChallengeResponse")]
[Route("ChallengeResponse")]
public class BT_ChallengeResponseController : Controller
{
    public BT_ChallengeResponseController(ILogger<BT_ChallengeResponseController> logger, IConfiguration configuration, IWebHostEnvironment hostingEnvironment)
    {
        ...
    }

    [Route("", Name = "default")]
    [Route("Index")]
    public IActionResult Index()
    {
        ...
    }

    [ResponseCache(Duration = 0, Location = ResponseCacheLocation.None, NoStore = true)]
    public IActionResult Error()
    {
        return View(new ErrorViewModel { RequestId = Activity.Current?.Id ?? HttpContext.TraceIdentifier });
    }
}

所以我不明白为什么它试图访问 Error 函数。我想要存档的是,我可以通过 https://localhost:8443/ChallengeResponse/Index 和 https://localhost:8443/ChallengeResponse

访问索引

我的 Program.cs 是:

if (Environment.GetEnvironmentVariable("ASPNETCORE_ENVIRONMENT") == "Development")
{
    // This will add live editing during debugging
    builder.Services.AddRazorPages()
        .AddRazorRuntimeCompilation();
} else {
    builder.Services.AddRazorPages();
}

var app = builder.Build();

// Configure the HTTP request pipeline.
if (!app.Environment.IsDevelopment())
{
    app.UseExceptionHandler("/Home/Error");
    app.UseForwardedHeaders();
    // The default HSTS value is 30 days. You may want to change this for production scenarios, see https://aka.ms/aspnetcore-hsts.
    app.UseHsts();
}
else
{
    app.UseDeveloperExceptionPage();
    app.UseForwardedHeaders();
}

app.UseHttpsRedirection();

app.UseStaticFiles();

app.UseRouting();

app.UseCertificateForwarding();

app.UseAuthentication();
app.UseAuthorization();
   
if (app.Environment.IsDevelopment())
{
    // To ignore the [Authorize] attribute on the controllers
    app.MapControllers().WithMetadata(new AllowAnonymousAttribute());
}

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

// https://stackoverflow.com/questions/59949443/how-to-fix-error-404-when-logging-out-on-an-asp-net-core-mvc-app-against-azure-a
app.MapRazorPages();

app.Run();

有人能指出我正确的方向吗? 谢谢

asp.net asp.net-core
1个回答
0
投票

当我将

[Route("ChallengeResponse")]
添加到我的任何控制器类时,我可以在我这边重现您的问题,因为它打破了定义为下面代码的默认映射规则。如果我们不向 Controller 类添加
[Route]
属性,则不会违反规则,那么操作方法上的
[Route]
属性将起作用。

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

解决方案之一是添加

[Route("Error")]

© www.soinside.com 2019 - 2024. All rights reserved.