这是我的主屏幕:
当我点击右侧的Log On
时,我明白了:
Server Error in '/' Application.
The resource cannot be found.
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: /HelloWorld/LogOn
Version Information: Microsoft .NET Framework Version:4.0.30319; ASP.NET Version:4.0.30319.17929
这是我在Visual Studio 2012中的项目层次结构:
这是_Layout.cshtml
内容:
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8" />
<title>@ViewBag.Title</title>
<link href="@Url.Content("~/Content/Site.css")" rel="stylesheet" type="text/css" />
<script src="@Url.Content("~/Scripts/jquery-1.5.1.min.js")" type="text/javascript"></script>
<script src="@Url.Content("~/Scripts/modernizr-1.7.min.js")" type="text/javascript"></script>
</head>
<body>
<div class="page">
<header>
<div id="title">
<h1>Welcome to our MVC Movies Application
</h1>
</div>
<div id="logindisplay">
<!-- My TEST Comment -->
<!--
From here we go into to log-on page , furthermore if the user is
not listed , then we go straight into registration
-->
[ <a href="/HelloWorld/LogOn">Log On</a> ] @*This is just a non-working stub that would be changed later on*@
</div>
<nav>
<ul id="menu">
<!-- format is as follows
[the name of the presented tab]
[the name of the file in the Search Solution]
[the folder in Search Solution where the file above is located]
-->
<li>@Html.ActionLink("Home Tab", "Index", "Movies")</li>
<li>@Html.ActionLink("Search Tab", "SearchIndex", "Movies")</li>
<li>@Html.ActionLink("Hello World Tab", "Index", "HelloWorld")</li>
</ul>
</nav>
</header>
<section id="main">
@RenderBody()
</section>
<footer>
</footer>
</div>
</body>
</html>
这是LogOn.cshtml
内容:
@model Mvc3ToolsUpdateWeb_Default.Models.LogOnModel
@{
ViewBag.Title = "LogOn";
}
<h2>Log On</h2>
<p>
Please enter your user name and password. @Html.ActionLink("Register", "Register") if you don't have an account.
</p>
<script src="@Url.Content("~/Scripts/jquery.validate.min.js")" type="text/javascript"></script>
<script src="@Url.Content("~/Scripts/jquery.validate.unobtrusive.min.js")" type="text/javascript"></script>
@Html.ValidationSummary(true, "Login was unsuccessful. Please correct the errors and try again.")
@using (Html.BeginForm()) {
<div>
<fieldset>
<legend>Account Information</legend>
<div class="editor-label">
@Html.LabelFor(m => m.UserName)
</div>
<div class="editor-field">
@Html.TextBoxFor(m => m.UserName)
@Html.ValidationMessageFor(m => m.UserName)
</div>
<div class="editor-label">
@Html.LabelFor(m => m.Password)
</div>
<div class="editor-field">
@Html.PasswordFor(m => m.Password)
@Html.ValidationMessageFor(m => m.Password)
</div>
<div class="editor-label">
@Html.CheckBoxFor(m => m.RememberMe)
@Html.LabelFor(m => m.RememberMe)
</div>
<p>
<input type="submit" value="Log On" />
</p>
</fieldset>
</div>
}
知道我在这里做错了什么吗?是什么导致404?
我猜布局文件中的路径有问题:
<a href="/HelloWorld/LogOn">Log On</a>
非常感谢
修改
HelloWorldController.cs:
using System.Web;
using System.Web.Mvc;
namespace MvcMovie.Controllers {
public class HelloWorldController : Controller {
public ActionResult Index() {
return View();
}
public ActionResult Welcome(string name, int numTimes = 1) {
ViewBag.Message = "Hello " + name;
ViewBag.NumTimes = numTimes;
return View();
}
}
}
答案 0 :(得分:1)
你需要添加这个......
using System.Web;
using System.Web.Mvc;
namespace MvcMovie.Controllers
{
public class HelloWorldController : Controller
{
public ActionResult Index()
{
return View();
}
public ActionResult Welcome(string name, int numTimes = 1)
{
ViewBag.Message = "Hello " + name;
ViewBag.NumTimes = numTimes;
return View();
}
[AllowAnonymous]
public ActionResult LogOn(string returnUrl)
{
ViewBag.ReturnUrl = returnUrl;
return View();
}
//
// POST: /Account/Login
[HttpPost]
[AllowAnonymous]
[ValidateAntiForgeryToken]
public ActionResult LogOn(LoginModel model, string returnUrl)
{
if (ModelState.IsValid && WebSecurity.Login(model.UserName, model.Password, persistCookie: model.RememberMe))
{
return RedirectToLocal(returnUrl);
}
// If we got this far, something failed, redisplay form
ModelState.AddModelError("", "The user name or password provided is incorrect.");
return View(model);
}
}
}
您需要一个操作来显示登录页面(如果需要)和一个接收用户凭据的操作。
编辑:我添加了新MVC项目的默认Login
操作。
答案 1 :(得分:0)
改为使用./HelloWorld
或../
。很多时候,服务器不接受/HelloWorld
答案 2 :(得分:0)
我猜您使用的是Visual Studio Development Server。您可以在解决方案资源管理器中的解决方案名称下右键单击项目。在那里,您可以选择IIS Express。