ASP Net MVC,“/”应用程序中的服务器错误 - 找不到资源
ASP Net MVC, Server Error in '/' Application - The resource cannot be found
我目前在尝试访问视图时遇到错误 "The Resource cannot be found"。
我试图打开的视图名为 "Register"。
控制器 (AccountController) 实现了 ActionResult 方法 "Register"。
同一个控制器包含一个 "Login" 视图,我可以毫无问题地访问它。
我要调用的网址:
本地主机:port/Account/Login <- 有效
本地主机:port/Account/Register <- 不起作用
控制器方法:
[HttpPost]
public ActionResult Register(LoginModel model)
{
if(ModelState.IsValid)
{
movieEntities db = new movieEntities();
var newUser = db.users.Create();
var encrypPass = Helpers.SHA256.Encode(model.Passwort);
var user = model.UserName;
newUser.user_name = user;
newUser.user_passwdhash = encrypPass;
db.users.Add(newUser);
db.SaveChanges();
return RedirectToAction("Index", "Home");
}
return View(model);
}
查看:
@model MovieDatabase.Models.LoginModel
@{
ViewBag.Title = "Register";
}
<h2>Register</h2>
@using (Html.BeginForm())
{
@Html.AntiForgeryToken()
<div class="form-horizontal">
<h4>LoginModel</h4>
<hr />
@Html.ValidationSummary(true, "", new { @class = "text-danger" })
<div class="form-group">
@Html.LabelFor(model => model.UserName, htmlAttributes: new { @class
= "control-label col-md-2" })
<div class="col-md-10">
@Html.EditorFor(model => model.UserName, new { htmlAttributes =
new { @class = "form-control" } })
@Html.ValidationMessageFor(model => model.UserName, "", new { @class = "text-danger" })
</div>
</div>
<div class="form-group">
@Html.LabelFor(model => model.Passwort, htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.EditorFor(model => model.Passwort, new { htmlAttributes = new { @class = "form-control" } })
@Html.ValidationMessageFor(model => model.Passwort, "", new { @class = "text-danger" })
</div>
</div>
<div class="form-group">
<div class="col-md-offset-2 col-md-10">
<input type="submit" value="Register="btn btn-default" />
</div>
</div>
</div>
}
@section Scripts {
@Scripts.Render("~/bundles/jqueryval")
}
我尝试清理、重建解决方案、重命名视图、重命名方法并创建新的...
我在这里错过了什么?
您正在调用的方法似乎是 POST 返回尝试创建另一个方法来 return 您所指的视图。将此添加到您的代码中。
public ActionResult Register()
{
return View();
}
The file name of the view is same of the name of your method. If
not you're getting the same error.
有关路由的更多信息,请参阅 Microsoft 文档 ASP.NET MVC Routing
我目前在尝试访问视图时遇到错误 "The Resource cannot be found"。
我试图打开的视图名为 "Register"。 控制器 (AccountController) 实现了 ActionResult 方法 "Register"。
同一个控制器包含一个 "Login" 视图,我可以毫无问题地访问它。
我要调用的网址:
本地主机:port/Account/Login <- 有效 本地主机:port/Account/Register <- 不起作用
控制器方法:
[HttpPost]
public ActionResult Register(LoginModel model)
{
if(ModelState.IsValid)
{
movieEntities db = new movieEntities();
var newUser = db.users.Create();
var encrypPass = Helpers.SHA256.Encode(model.Passwort);
var user = model.UserName;
newUser.user_name = user;
newUser.user_passwdhash = encrypPass;
db.users.Add(newUser);
db.SaveChanges();
return RedirectToAction("Index", "Home");
}
return View(model);
}
查看:
@model MovieDatabase.Models.LoginModel
@{
ViewBag.Title = "Register";
}
<h2>Register</h2>
@using (Html.BeginForm())
{
@Html.AntiForgeryToken()
<div class="form-horizontal">
<h4>LoginModel</h4>
<hr />
@Html.ValidationSummary(true, "", new { @class = "text-danger" })
<div class="form-group">
@Html.LabelFor(model => model.UserName, htmlAttributes: new { @class
= "control-label col-md-2" })
<div class="col-md-10">
@Html.EditorFor(model => model.UserName, new { htmlAttributes =
new { @class = "form-control" } })
@Html.ValidationMessageFor(model => model.UserName, "", new { @class = "text-danger" })
</div>
</div>
<div class="form-group">
@Html.LabelFor(model => model.Passwort, htmlAttributes: new { @class = "control-label col-md-2" })
<div class="col-md-10">
@Html.EditorFor(model => model.Passwort, new { htmlAttributes = new { @class = "form-control" } })
@Html.ValidationMessageFor(model => model.Passwort, "", new { @class = "text-danger" })
</div>
</div>
<div class="form-group">
<div class="col-md-offset-2 col-md-10">
<input type="submit" value="Register="btn btn-default" />
</div>
</div>
</div>
}
@section Scripts {
@Scripts.Render("~/bundles/jqueryval")
}
我尝试清理、重建解决方案、重命名视图、重命名方法并创建新的... 我在这里错过了什么?
您正在调用的方法似乎是 POST 返回尝试创建另一个方法来 return 您所指的视图。将此添加到您的代码中。
public ActionResult Register()
{
return View();
}
The file name of the view is same of the name of your method. If not you're getting the same error.
有关路由的更多信息,请参阅 Microsoft 文档 ASP.NET MVC Routing