Click here to Skip to main content
15,903,175 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
So I have an ASP.NET Core project with a Razor page called Index.cshtml which contains a form that has input fields for accepting the name and query of a user. I have declared all the asp fields required to point the right controller to that page in the form tag. When I submit my form I keep getting the error that http error 400 this page is not working now. The form section, the controller and the Startup.cs code are listed in the code section below. Help me get rid of that error.

What I have tried:

using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;

namespace ASPLearner.Controllers
    [Route("api/[controller]",Name = "Form")]
    public class FormController : ControllerBase
        public IActionResult GetAction(string name, string query)
            var filePath = Path.Combine("file.txt");

            // Construct the data to write
            var data = $"Name: {name}\nEmail: {query}\n";

            // Write the data to the file
            System.IO.File.WriteAllText(filePath, data);
            // Redirect to a success page or return a response
            return RedirectToAction("Success");

The HTML code for the form
 <form onsubmit="return false;" method="post" asp-route="Form" asp-controller="FormController"  asp-action="GetAction" id="form">
                            <div class="form-group">
                                <label for="name">Name</label>
                                <input type="text" name="name" class="form-control" id="name" placeholder="Enter your name" asp-for="Name">
                            <div class="form-group">
                                <label for="query">Query</label>
                                <textarea class="form-control" name="query" id="query" style="margin-top:20px" rows="5" placeholder="Enter your query" asp-for="Description"></textarea>

using Microsoft.Extensions.DependencyInjection;
var builder = WebApplication.CreateBuilder(args);
//configure the services before calling the
IServiceCollection services = builder.Services;
//add controllers with views
//add all the razor pages
var app = builder.Build();
//alow static styling files to be loaded from wwwroot
//add https redirection
//add routing for the razor pages
app.MapControllerRoute(name: "default", pattern: "{controller=Home}/{action=Index}/{id?}");
Updated 6-Jul-23 7:17am
Dave Kreskowiak 20-Jun-23 9:18am    
Delete the other, duplicate question.
Tim the Gamer 20-Jun-23 9:20am    
can you share a link to it
Dave Kreskowiak 20-Jun-23 10:05am    
Nope. An admin deleted it already.
Tim the Gamer 20-Jun-23 15:47pm    
the modal is appending a # to the url, could this be the cause as the url ending with # is not mapped?

Adding this [APIController] to the Form Controller class fixed the issue. In the form I used the asp-route atrribute to the name of the route in the controller and it works. In the form you only need the attribute asp-route which needs the name you passed to the route when you were defining the controller class like below.
[Route("api/controller", Name="Form" )] 
public class FormController : ControllerBase
 //define a method of your choice where the parameters match the names of the input fields in the form
 public IActionResult Get([FromForm] string Name,[From Form] string Query) {
return Ok("Success") ;

Then in your HTML file where the form lives, just make sure the form
Uses the tag asp-route name we defined
<form method="post" asp-route="Form" >
<input name="Name" type="text" />
<textarea name="Query"></textarea>
<input type="submit">
Share this answer
Andre Oosthuizen 21-Jun-23 5:12am    
Please share the link to the '[APIController]' for future readers. Also explain and show the asp-route attribute method you used, thanks.
Tim the Gamer 21-Jun-23 5:22am    
Updated my answer though I did not see any code formatting tool. Can you format it for me please
Andre Oosthuizen 21-Jun-23 5:25am    
Thanks and done, +5
Tim the Gamer 21-Jun-23 5:26am    
Thanks. Now am moving to the next stage where I need to create a database migration and save the input data to a database on the server and so on and so forth. Asp is easier than I thought
Andre Oosthuizen 21-Jun-23 5:42am    
Best of luck! 'Asp is easier than I thought' - until the **** hits the proverbial fan... :)
I searched for a similar post on Stackoverflow and adding the line below fixes the issue where submitting a form causes a HTTP Error 400 to be returned. Add this code to your Form

Share this answer

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900