Server Error in '/' Application. ASP.Net MVC Visual Studio 2019
Whenever I run the program, it works perfectly. However, as soon as I hit the "Login" button it tells me
Description: HTTP 404. The resource you are looking for (or one of its dependencies) could have been removed, had its name changed, or is temporarily unavailable. Please review the following URL and make sure that it is spelled correctly.
Requested URL: /LoginController/Authorize
I checked everything and the spelling is correct. I am new to this and cannot figure out what I am doing wrong. Any guidance would be highly appreciated it.
Inside of the App_Start
folder I have my RouteConfig.cs
file. It contains the following:
namespace CoffeeShop_Web_App { public class RouteConfig { public static void RegisterRoutes(RouteCollection routes) { routes.IgnoreRoute("{resource}.axd/{*pathInfo}"); routes.MapRoute( name: "Default", url: "{controller}/{action}/{id}", defaults: new { controller = "LoginController", action = "Index", id = UrlParameter.Optional } ); } } }
I have one controller which is my LoginController.cs
which contains the following.
namespace CoffeeShop_Web_App.Controllers { public class LoginController : Controller { // GET: Login public ActionResult Index() { return View(); } [HttpPost] public ActionResult Authorize() { return View(); } } }
Lastly, my only view Index.cshtml
which contains the following.
@model CoffeeShop_Web_App.Models.OwnerLogin @{ Layout = null; } <!DOCTYPE html> <html> <head> <meta name="viewport" content="width=device-width" /> <title>Login</title> <style> #login-div { position: absolute; left: 40%; top: 40%; border: 1px solid #ccc; padding: 10px 10px; } </style> </head> <body> <div id="login-div"> @using (Html.BeginForm("Authorize", "LoginController", FormMethod.Post)) { <table> <tr> <td></td> <td style="text-decoration:underline">Coffee Shop</td> </tr> <tr> <td> @Html.LabelFor(model => model.USERNAME) </td> <td> @Html.EditorFor(model => model.USERNAME) </td> </tr> <tr> <td></td> <td>@Html.ValidationMessageFor(model => model.USERNAME)</td> </tr> <tr> <td> @Html.LabelFor(model => model.PASSWORD) </td> <td> @Html.EditorFor(model => model.PASSWORD) </td> </tr> <tr> <td></td> <td>@Html.ValidationMessageFor(model => model.PASSWORD)</td> </tr> <tr> <td></td> <td> <input type="submit" name="name" value="Login" /> <input type="reset" name="name" value="Clear" /> </td> </tr> </table> } </div> <script src="~/Scripts/jquery-3.4.1.min.js"></script> <script src="~/Scripts/jquery.validate.min.js"></script> <script src="~/Scripts/jquery.validate.unobtrusive.min.js"></script> </body> </html>
Your form is posting back to the Authorize
method of LoginController
controller:
@using (Html.BeginForm("Authorize", "LoginController", FormMethod.Post))
You don’t have to specify controller suffix for the name of the controller. The following should fix it:
@using (Html.BeginForm("Authorize", "Login", FormMethod.Post))
Also you did the same mistake when setting up the routing too:
defaults: new { controller = "LoginController", action = "Index", id = UrlParameter.Optional }
which should have been just:
defaults: new { controller = "Login", action = "Index", id = UrlParameter.Optional }