我是MVC的新手。我正在尝试创建一个简单的用户注册过程
当我尝试运行应用程序时,我遇到错误HTTP 404。 无法找到该资源。 请求的URL:/Mvc_Web_Project/Views/CreateNewUser/AddUser.cshtml
我在网上找到的解决方案没有解决问题
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>
}
答案 0 :(得分:2)
您的ASP.NET Web应用程序的开始操作似乎设置为当前页面,在您的情况下,这会导致Visual Studio启动浏览器导航到/Mvc_Web_Project/Views/CreateNewUser/AddUser.cshtml
路径。
要更改此设置,一个选项是转到“项目属性”窗口并从当前页面更改为特定页面并使用空值,这将导致浏览器始终导航到应用程序的根目录:
以下是docs的链接,可以更详细地解释这些选项。