【问题标题】:Controller Method not found -AmbiguousActionException找不到控制器方法 -AmbiguousActionException
【发布时间】:2020-04-09 08:20:26
【问题描述】:

我正在尝试调用控制器的 httpget 方法。但它总是 404。我已经添加了所有必需的指令。当我直接调用类名时,我得到了这个异常

AmbiguousActionException: Multiple actions matched. The following actions matched route data and had all constraints satisfied:

控制器

using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Mvc;

// For more information on enabling Web API for empty projects, visit https://go.microsoft.com/fwlink/?LinkID=397860

namespace WebApplication2.Controllers
{
    [Route("api/[controller]")]
    public class ScannerController : Controller
    {

        [HttpGet]
        public async Task<IActionResult> dostuff(string file_id)
        {
            return null;
        }
    }
}

Startup.cs

using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Hosting;
using Microsoft.AspNetCore.HttpsPolicy;
using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.SpaServices.AngularCli;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Options;
using WebApplication2.Models;

namespace WebApplication2
{
    public class Startup
    {
        public Startup(IConfiguration configuration)
        {

            Configuration = configuration;
        }

        public IConfiguration Configuration { get; }

        // This method gets called by the runtime. Use this method to add services to the container.
        public void ConfigureServices(IServiceCollection services)
        {
            services.AddMvc().SetCompatibilityVersion(CompatibilityVersion.Version_2_1);

            // In production, the Angular files will be served from this directory
            services.AddSpaStaticFiles(configuration =>
            {
                configuration.RootPath = "ClientApp/dist";
            });

            // requires using Microsoft.Extensions.Options
            services.Configure<DatabaseSettings>(
                Configuration.GetSection(nameof(DatabaseSettings)));

            services.AddSingleton<IDatabaseSettings>(sp =>
                sp.GetRequiredService<IOptions<DatabaseSettings>>().Value);


        }

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

            app.UseHttpsRedirection();
            app.UseStaticFiles();
            //app.UseSpaStaticFiles();


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

            app.UseSpa(spa =>
            {
                // To learn more about options for serving an Angular SPA from ASP.NET Core,
                // see https://go.microsoft.com/fwlink/?linkid=864501

                spa.Options.SourcePath = "ClientApp";

                if (env.IsDevelopment())
                {
                    spa.UseProxyToSpaDevelopmentServer("http://localhost:4200");

                }
            });
        }
    }
}

【问题讨论】:

  • 你在调用 GET api/scanner/dostuff?
  • 这看起来很奇怪 [Route("api/[controller]")]
  • @RicardoPeres 是的

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


【解决方案1】:

dostuff方法上的路由改为:

[HttpGet("dostuff")]

【讨论】:

    猜你喜欢
    • 2014-04-10
    • 1970-01-01
    • 2014-04-29
    • 1970-01-01
    • 2023-01-27
    • 2015-10-09
    • 2016-04-04
    • 2014-03-09
    • 2014-05-12
    相关资源
    最近更新 更多