我有以下控制器:
public class ItemController : Controller
{
private SomeDbEntities _db= new SomeDbEntities ();
//
// GET: /Item/
public ActionResult Index()
{
var items = _db.Items.ToList();
return View(items);
}
public ActionResult Decription(int id)
{
var item = _db.Items.Single(a => a.ItemID == id);
return View(item);
}
}
和 2 个视图:
索引.cshmtl
@model IEnumerable<Web.Models.Item>
@{
ViewBag.Title = "Index";
}
<h2>Index</h2>
<ul>
@foreach (var item in Model)
{
<li>@Html.ActionLink(item.Name, "Description", new { id = item.ItemID})</li>
}
说明.cshtml
@model Web.Models.Item
@{
ViewBag.Title = "Decription";
}
<h2>Decription</h2>
<p>@Model.Description</p>
路线
public static void RegisterRoutes(RouteCollection routes)
{
routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
routes.MapRoute(
name: "Default",
url: "{controller}/{action}/{id}",
defaults: new { controller = "Item", action = "Index", id = UrlParameter.Optional }
);
routes.MapHttpRoute(
name: "DefaultApi",
routeTemplate: "api/{controller}/{id}",
defaults: new { id = RouteParameter.Optional }
);
}
当我尝试访问时,即http://localhost:12952/Item/Description/2
出现服务器错误:
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.