Reputation: 8783
I have migrated to ASP.NET Core 2.1
.I'd like to submit a form (Insert method from ArticleController: url: http://localhost:52125/article/insert) but I got this error:
An unhandled exception occurred while processing the request. AmbiguousActionException: Multiple actions matched. The following actions matched route data and had all constraints satisfied:
Jahan.Beta.Web.App.Controllers.ArticleController.Insert (Jahan.Beta.Web.App) Jahan.Beta.Web.App.Controllers.ArticleController.Edit (Jahan.Beta.Web.App) Jahan.Beta.Web.App.Controllers.ArticleController.DeleteConfirmed (Jahan.Beta.Web.App) Microsoft.AspNetCore.Mvc.Internal.ActionSelector.SelectBestCandidate(RouteContext context, IReadOnlyList candidates)
How can I solve it? and is there a way to improve/optimize routes.MapRoute?
Article Controller:
[Route("[controller]")]
public class ArticleController : Controller
{
[HttpPost]
public async Task<IActionResult> Insert([FromBody]Article article)
{
// I want to run this method!
}
[HttpGet("Edit/{id}")]
public async Task<IActionResult> Edit(int? id)
{
}
[HttpPost]
[ValidateAntiForgeryToken]
public async Task<IActionResult> Edit(int id, [FromBody] Article article)
{
}
[HttpPost, ActionName("DeleteConfirmed/{id}")]
public async Task<IActionResult> DeleteConfirmed(int id)
{
}
}
a part of startup.cs file:
public void Configure(IApplicationBuilder app, IHostingEnvironment env)
{
app.UseDeveloperExceptionPage();
app.UseStatusCodePages();
app.UseStaticFiles();
app.UseAuthentication();
var provider = new FileExtensionContentTypeProvider();
provider.Mappings[".less"] = "plain/text";
app.UseStaticFiles(new StaticFileOptions
{
ContentTypeProvider = provider
});
ApplicationDbContext.CreateAdminAccount(app.ApplicationServices, Configuration).Wait();
app.UseMvc(routes =>
{
routes.MapRoute(
name: "areas",
template: "{area:exists}/{controller=Home}/{action=Index}/{id?}"
);
routes.MapRoute(
name: "AdminAreasUsers",
template: "{area:exists}/{controller=Users}/{action=Index}/{id?}"
);
routes.MapRoute(
name: "AdminAreasDeleteUser",
template: "{area:exists}/{controller=Users}/{action=Delete}/{id}"
);
routes.MapRoute(
name: "FilterByTagAsync",
template: "{controller}/{action}/{tagId}/{articlePage}",
defaults: new { controller = "Article", action = "FilterByTagAsync", tagId = "", articlePage = "" }
);
routes.MapRoute(
name: "ShowArticle",
template: "{controller}/{action}/{Id}",
defaults: new { controller = "Article", action = "Show", Id = "" }
);
routes.MapRoute(
name: "default",
template: "{controller}/{action}",
defaults: new { controller = "Home", action = "Index" }
);
routes.MapRoute(
name: "About",
template: "{Controller=AboutUs}/{action=AboutUs}/{id?}");
});
if (env.IsDevelopment())
{
app.UseDeveloperExceptionPage();
}
else
{
app.UseExceptionHandler("/Error");
app.UseHsts();
}
}
Upvotes: 0
Views: 1747
Reputation: 239290
You're using attribute-based routing, but haven't applied any routes to any of those actions. As such, they're all using the default of ""
, or an empty route. You need to do something like:
[HttpPost("insert")]
public async Task<IActionResult> Insert([FromBody]Article article)
[HttpGet("Edit/{id}")]
public async Task<IActionResult> Edit(int? id)
[HttpPost("edit/{id}")]
[ValidateAntiForgeryToken]
public async Task<IActionResult> Edit(int id, [FromBody] Article article)
[HttpPost("delete"), ActionName("DeleteConfirmed/{id}")]
public async Task<IActionResult> DeleteConfirmed(int id)
Upvotes: 2