Error 404 when pressing button

chairmanPC

Active member
Joined
Apr 19, 2021
Messages
38
Programming Experience
10+
This is my first ASP.NET Core project. I started with adding a button that will check if it is clicked. It will print a YES when it's clicked. but when I hit the button, I got into this Error 404 page. What did I do wrongly?

Index page:
@{
    ViewData["Title"] = "Index";
}

<form class="text-center">
    <button name="button" asp-action="checkValue">Check if Pressed</button>
</form>
<div class="text-center">
    Status: @TempData["status"]
</div>

And this below is my btnCheckController.
btnCheckController:
namespace firstASPNETApp.Controllers
{
    public class btnCheckController : Controller
    {
        public IActionResult Index()
        {
            return View();
        }

        public IActionResult checkValue()
        {
            TempData["status"] = "YES";
            return RedirectToAction("Index");
        }
    }
}

}

Where did I do wrongly?
 
Solution
What was the URL on the browser before clicked on the button?
What was the URL on the browser when you got the 404 error?
I fixed it. I realized wrote the method in the buttonController file, rather than the homeController file where I should have written it. My index page is my default starting page, so when I clicked the button, the error yelled at me that it cannot find the method, because I did not write it in the homeController file. As soon as I moved the block of code into the homeController file, the web page was able to find the function, and it displayed correctly.
What was the URL on the browser before clicked on the button?
What was the URL on the browser when you got the 404 error?
 
What was the URL on the browser before clicked on the button?
What was the URL on the browser when you got the 404 error?
I fixed it. I realized wrote the method in the buttonController file, rather than the homeController file where I should have written it. My index page is my default starting page, so when I clicked the button, the error yelled at me that it cannot find the method, because I did not write it in the homeController file. As soon as I moved the block of code into the homeController file, the web page was able to find the function, and it displayed correctly.
 
Solution
Back
Top Bottom