c# - 学习 ASP.Net MVC 并构建用户管理器 '/' 应用程序中的服务器错误

标签 c# asp.net asp.net-mvc

对于我的一门类(class),我刚刚开始学习如何在 ASP.Net MVC5 中开发 Web 应用程序。我们需要做的一件事是重新设计一个简单的用户帐户管理器的 View ,其中已经提供给我们使用和修改的代码。

问题是,当我在 Visual Studio 中实际运行代码时,出现 404 错误,提示 “/”应用程序中的服务器错误

我相当确定代码在类里面展示时是有效的,但我无法让它在这里工作。

下面是示例代码,感兴趣的可以引用:

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 })
}

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

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: /Views/index.cshtml

Version Information: Microsoft .NET Framework Version:4.0.30319; ASP.NET Version:4.0.30319.34274

最佳答案

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

正如我们在您的错误中看到的,请求的 URL 是 /Views/index.cshtml

这个错误只是意味着,应用程序找不到任何路由。 您可以检查 URL yourdomain/UserManager/Index

或者你可以在RouteConfig中设置默认 Action 来改变

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

这也是ASP.NET Routing的好文档

希望对您有所帮助!

关于c# - 学习 ASP.Net MVC 并构建用户管理器 '/' 应用程序中的服务器错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40561366/

相关文章:

c# - 表 __MigrationHistory 未更新

c# - 每次调用 Equals 都会返回 false,即使值相同 (C#)

c# - 窗体 : How to bind the Checkbox item of a CheckedListBox with databinding

c# - 在javascript中访问C#变量

asp.net - 在转发器项目中使用 JQuery 更新/删除数据库记录

asp.net-mvc - 从 $.Ajax Post 返回 PartialView

c# - 使用自定义 Razor View 引擎处理布局属性

c# - 如何调用顶级构造函数?

c# - System.IdentityModel.Tokens.JwtSecurityToken 自定义属性

javascript - 为什么我无法从 AngularJS 甚至纯 JavaScript 获取 __RequestVerificationToken 的值?