名称冲突后,ASP.NET Core MVC路由到控制器不起作用

时间:2018-10-11 08:48:04

标签: c# asp.net-core asp.net-core-routing

我最初有2个名称为“ AgreementController”的控制器,但其中一个位于“ API”命名空间中。使用以下设置,这会导致路由错误:

app.UseMvc(routes =>
        {
            routes.MapRoute(
                name: "default",
                template: "{controller=Home}/{action=Index}/{id?}");
            routes.MapRoute(
                name: "ActionApi",
                template: "api/{controller}/{action}/{id?}");
        });

为了解决这个问题,我将“ API”命名空间中的名称重命名为“ AgreementAPIController”,但是由于某些原因,即使控制器看起来像这样,我仍然无法路由到“ / Agreement / Create /?customerId = 1234”像这样:

public class AgreementController : Controller
{
    private readonly IMapper _mapper;


    public AgreementController(IMapper mapper)
    {
        _mapper = mapper;
    }

    [HttpGet]
    public IActionResult Create(int customerId)
    {
        return View(customerId);
    }        
}

public class AgreementAPIController : Controller
{
    private readonly IMapper _mapper;

    public AgreementAPIController(IMapper mapper)
    {
        _mapper = mapper;
    }

    [HttpGet]
    public IActionResult Test()
    {
        return View();
    }

如果我也将“ AgreementController”重命名为“ AgreementXController”,则一切正常。我为什么要重命名两个控制器?是否存在某种保留旧名称的缓存?

这是完整的Startup.cs文件。

public class Startup
{
    public Startup(IConfiguration configuration, IHostingEnvironment environment)
    {
        Configuration = configuration;
        Environment = environment;
    }

    public IConfiguration Configuration { get; }
    public IHostingEnvironment Environment { get; }

    // This method gets called by the runtime. Use this method to add services to the container.
    public void ConfigureServices(IServiceCollection services)
    {
        services.AddScoped<IRFDbRepository, RFDbRepository>();
        var connection = Configuration.GetConnectionString("RFDbConnection");
        services.Configure<ConnectionStrings>(Configuration.GetSection("ConnectionStrings"));
        services.AddDbContext<RFDbContext>(options => options.UseSqlServer(connection));
        services.AddDbContext<IdentityDbContext>(options => options.UseSqlServer(connection));
        services.AddIdentity<User, UserRole>().AddDefaultTokenProviders();
        services.AddTransient<IUserStore<User>, UserStore>();
        services.AddTransient<IRoleStore<UserRole>, RoleStore>();
        services.AddAutoMapper();

        services.AddAuthorization(x =>
        {
            if (Configuration.GetValue<bool>("DisableAuthentication") && Environment.IsDevelopment())
            {
                x.DefaultPolicy = new AuthorizationPolicyBuilder()
                    .RequireAssertion(_ => true)
                    .Build();
            }
        });

        services.AddMvc().SetCompatibilityVersion(CompatibilityVersion.Version_2_1).AddRazorPagesOptions(options =>
        {
            options.AllowAreas = true;
            options.Conventions.AuthorizeAreaPage("Identity", "/Account/Logout");
        });

        services.ConfigureApplicationCookie(options =>
        {
            options.Cookie.HttpOnly = true;
            options.LoginPath = "/Identity/Account/Login";
            options.LogoutPath = "/Identity/Account/Logout";
            options.ExpireTimeSpan = TimeSpan.FromMinutes(Configuration.GetValue<int>("AuthenticationCookie:ExpiryMinutes"));
            options.SlidingExpiration = Configuration.GetValue<bool>("AuthenticationCookie:SlidingExpiration");
        });
    }

    // This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
    public void Configure(IApplicationBuilder app, IHostingEnvironment env, ILoggerFactory loggerFactory, IRFDbRepository rFDbRepository)
    {
        if (env.IsDevelopment())
        {
            app.UseDeveloperExceptionPage();
        }
        else
        {
            app.UseExceptionHandler("/Home/Error");
        }

        loggerFactory.AddFile(Configuration.GetValue<string>("Logging:LogFile"));
        app.UseStaticFiles();

        app.UseAuthentication();

        app.UseMvc(routes =>
        {
            routes.MapRoute(
                name: "default",
                template: "{controller=Home}/{action=Index}/{id?}");
            routes.MapRoute(
                name: "ActionApi",
                template: "api/{controller}/{action}/{id?}");
        });

        rFDbRepository.TestConnection();
    }
}

0 个答案:

没有答案