Reputation: 133
I am new to MVC. I am trying to create a simple user registration process
When I try to run the application I have the error HTTP 404. The resource cannot be found. Requested URL: /Mvc_Web_Project/Views/CreateNewUser/AddUser.cshtml
Solution i found online did not solve the issue
RouteConfig.cs
public static void RegisterRoutes(RouteCollection routes)
{
routes.MapRoute(
name: "Default",
url: "{controller}/{action}/{id}",
defaults: new { controller = "CreateNewUser", action = "AddUser", id = UrlParameter.Optional }
);
}
Controller.cs
using System.Web.Mvc;
using System.Data.SqlClient;
using System.Configuration;
using System.Data;
using Mvc_Web_Project.Models;
namespace Mvc_Web_Project.Controllers
{
public class CreateNewUserController : Controller
{
private SqlConnection con;
private void connection()
{
string constr = ConfigurationManager.ConnectionStrings["Molecular"].ToString();
con = new SqlConnection(constr);
}
// GET: CreateNewUser
public ActionResult AddUser(AddUserModel CreateNewUser)
{
//To Prevent firing validation error on first page Load
if (ModelState.IsValid)
{
connection();
SqlCommand com = new SqlCommand("AddUser", con);
com.CommandType = CommandType.StoredProcedure;
com.Parameters.AddWithValue("@Email", CreateNewUser.Email);
com.Parameters.AddWithValue("@Password", CreateNewUser.Password);
com.Parameters.AddWithValue("@Type", CreateNewUser.Type);
con.Open();
int i = com.ExecuteNonQuery();
con.Close();
if (i >= 1)
{
ViewBag.Message = "New User Added Successfully";
}
}
ModelState.Clear();
return View();
}
}
}
cshtml
@model Mvc_Web_Project.Models.AddUserModel
@{
ViewBag.Title = "Create New User";
Layout = "~/Views/Shared/_Layout.cshtml";
}
<h2>AddUser</h2>
@using (Html.BeginForm())
{
@Html.ValidationSummary(true)
<fieldset>
<legend> AddUserModel</legend>
<div class="editor-label">
@Html.LabelFor(model => model.Email)
</div>
<div class="editor-field">
@Html.EditorFor(model => model.Email)
@Html.ValidationMessageFor(model => model.Email)
</div>
<div class="editor-label">
@Html.LabelFor(model => model.Password)
</div>
<div class="editor-field">
@Html.EditorFor(model => model.Password)
@Html.ValidationMessageFor(model => model.Password)
</div>
<div class="editor-label">
@Html.LabelFor(model => model.Type)
</div>
<div class="editor-field">
@Html.EditorFor(model => model.Type)
@Html.ValidationMessageFor(model => model.Type)
</div>
<p>
<input type="submit" value="Save details" />
</p>
</fieldset>
}
Upvotes: 0
Views: 1764
Reputation: 93003
It looks like the Start Action for your ASP.NET Web Application is set to Current Page, which, in your case, is causing Visual Studio to launch a browser that navigates to the /Mvc_Web_Project/Views/CreateNewUser/AddUser.cshtml
path.
To change this, one option is to go to the Project Properties window and change from Current Page to Specific Page and use an empty value, which will cause the browser to always navigate to the root of the application:
Here's a link to the docs that explains the options in more detail.
Upvotes: 2