0

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>
 }
3
  • When you run the application, which file do you have open in Visual Studio? Commented Nov 13, 2017 at 13:40
  • @Kirk These file are open in Visual Studio (AddUser.cshtml, RouteConfig.cs, CreateNewuserController.cs, AddUserModel.cs) Commented Nov 13, 2017 at 13:44
  • Go to the controller file (controller file should be active file) in VS and then run the application. It will open correct URL. Commented Nov 13, 2017 at 13:50

1 Answer 1

2

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:

enter image description here

Here's a link to the docs that explains the options in more detail.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.