出现HTTP ERROR 405错误通常是由于未正确配置应用程序的路由或动作方法。为了解决这个问题,您可以按照以下步骤进行操作:
确认您的表单方法已正确定义为POST。例如,您可以在视图中使用@using(Html.BeginForm("ActionName", "ControllerName", FormMethod.Post))。
检查您的Controller类中是否包含具有相应名称和参数的动作方法。例如,如果您的表单在“ActionName”中提交,则需要一个名为“ActionName”的HttpPost动作方法。
如果您的表单数据模型中包含复杂类型(例如对象或列表),则需要使用[FromBody]属性将请求正文中的数据绑定到该类型。例如,您可以在动作方法中使用[HttpPost]和[FromBody]属性:
[HttpPost] public IActionResult ActionName([FromBody] MyModel model) { // your code here }
app.UseEndpoints(endpoints => { endpoints.MapControllerRoute( name: "default", pattern: "{controller=Home}/{action=Index}/{id?}"); });
$(document).on('submit', 'form', function(e) { e.preventDefault(); $.ajax({ url: $(this).attr('action'), type: $(this).attr('method'), data: $(this).serialize(), success: function(response) { // your code here }, error: function(jqXHR, textStatus, errorThrown) { // your code here } }); });
通过这些步骤,您应该能够在ASP .Net Core 3.1中成功提交表单并避免HTTP ERROR 405错误。