学习ASP.'/'应用程序

本文关键字:应用程序 ASP 学习 | 更新日期: 2023-09-27 18:17:46

对于我的一门课,我刚刚开始学习如何在ASP中开发web应用程序。净MVC5。我们需要做的一件事是重新设计一个简单的用户帐户管理器的视图,使用已经提供给我们使用和修改的代码。

问题是,当我在Visual Studio中实际运行代码时,我得到404错误,说Server Error in '/' Application .

我很确定在课堂上展示的代码可以工作,但是我不能让它在这里工作。

下面是示例代码,供感兴趣的人参考:

UserManagerController.cs

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
namespace CS610W6.Controllers
{
    public class UserManagerController : Controller
    {
        //
        // GET: /UserManager/
        public ActionResult Index()
        {
            //Use the application's DB context to access the Identity framework's users
            var UserList = new CS610W6.Models.ApplicationDbContext().Users.ToList();
            //Pass the UserList to the view
            return View(UserList);
        }
    }
 }

index.cshtml

@model List<CS610W6.Models.ApplicationUser>
@{
    ViewBag.Title = "Index";
}
<h2>Index</h2>
@foreach (var item in Model)
{
    @item.UserName<br />
    @item.PasswordHash<br />
    @item.Id<br />
    @Html.ActionLink("Edit", "Edit", new { id = item.Id })
}

编辑:这里是错误的全文,如浏览器所示,

'/'应用程序出现服务器错误。

无法找到资源。

描述:HTTP 404。您正在查找的资源(或其中之一)依赖项)可以被删除,如果它的名称被更改,或者是暂时不可用。请查看以下网址并制作确保拼写正确。

请求的URL:/Views/index.cshtml

版本信息:Microsoft .NET Framework Version:4.0.30319;ASP。净版:4.0.30319.34274

学习ASP.'/'应用程序

由于您是MVC新手,我假设您在第一次尝试运行应用程序时会出现此错误。

我们可以看到您的错误,请求的URL是/Views/index.cshtml

这个错误仅仅表示应用程序找不到任何路由。您可以使用URL yourdomain/UserManager/Index

检查。

或者您可以设置默认操作来更改RouteConfig

 routes.MapRoute(
                name: "Default",
                url: "{controller}/{action}/{id}",
                defaults: new { controller = "UserManager", action = "Index", id = UrlParameter.Optional }
            );

这里也是一个很好的ASP文档。. NET路由

希望这对你有帮助!