C# ASP.NET核心MVC-localhost当前无法处理此请求

C# ASP.NET核心MVC-localhost当前无法处理此请求,c#,asp.net-core-mvc,C#,Asp.net Core Mvc,我有一个简单的应用程序。 Index.cshtml页面显示一个登录页面,该页面绑定了用户模型。无论何时从Index.cshtml页面提交登录表单,都会调用控制器httppost操作方法login,该方法只需查找正确的用户名和密码,如果用户在场,它只需将用户重定向到login.cshtml视图,并显示如下所示的用户列表: 索引C#代码: public IActionResult Index() //simply return the login page view {

我有一个简单的应用程序。
Index.cshtml页面显示一个登录页面,该页面绑定了用户模型。无论何时从Index.cshtml页面提交登录表单,都会调用控制器httppost操作方法login,该方法只需查找正确的用户名和密码,如果用户在场,它只需将用户重定向到login.cshtml视图,并显示如下所示的用户列表:

索引C#代码:

 public IActionResult Index()  //simply return the login page view
    {
        return View();
    }    
[HttpPost]
    public IActionResult Login(User user)
    {
        UserProvider userProvider = new UserProvider();
        User user1 = userProvider.CreateUsersList().Where(x => x.Username == user.Username && x.Password == user.Password).FirstOrDefault();
        TempData["CurrentUser"] = user1;
        if (user1 == null)
        {
            ViewData.Clear();
            return View("Index");
        }
        List<User> users = userProvider.CreateUsersList().Where(x => x.Id != user1.Id).ToList();
        return View(users);
    }    
Index.cshtml

@model NewChatApp.Models.User    

<div class="wrapper fadeInDown">
<div id="formContent" class="mt-5">
    <form method="post" class="mt-5" asp-action="Login">
        <input hidden="hidden" asp-for="Id" />
        <input hidden="hidden" asp-for="Name" />
        <input type="text" class="fadeIn second" placeholder="login" asp-for="Username">
        <input type="text" class="fadeIn third" placeholder="password" asp-for="Password">
        <input type="submit" class="fadeIn fourth mt-3" value="Log In">
    </form>

</div>
@model NewChatApp.Models.User
登录C#代码:

 public IActionResult Index()  //simply return the login page view
    {
        return View();
    }    
[HttpPost]
    public IActionResult Login(User user)
    {
        UserProvider userProvider = new UserProvider();
        User user1 = userProvider.CreateUsersList().Where(x => x.Username == user.Username && x.Password == user.Password).FirstOrDefault();
        TempData["CurrentUser"] = user1;
        if (user1 == null)
        {
            ViewData.Clear();
            return View("Index");
        }
        List<User> users = userProvider.CreateUsersList().Where(x => x.Id != user1.Id).ToList();
        return View(users);
    }    
[HttpPost]
公共IActionResult登录(用户)
{
UserProvider UserProvider=新的UserProvider();
User user1=userProvider.CreateUsersList()。其中(x=>x.Username==User.Username&&x.Password==User.Password)。FirstOrDefault();
TempData[“CurrentUser”]=user1;
if(user1==null)
{
ViewData.Clear();
返回视图(“索引”);
}
List users=userProvider.CreateUsersList()。其中(x=>x.Id!=user1.Id).ToList();
返回视图(用户);
}    
Login.cshtml代码

@model IEnumerable<NewChatApp.Models.User>

@{
   Layout = "~/Views/Shared/_Layout.cshtml";
}

<div class="col-md-3">
<table class="table">
    <thead>
        <tr>
            <th>
                FRIENDS
            </th>
        </tr>
    </thead>
    <tbody>
        @foreach (User item in Model)
        {
            <tr>
                <td>
                    <button class="btn btn-link">@item.Name</button>
                </td>
            </tr>
        }
    </tbody>
</table>
@model IEnumerable
@{
Layout=“~/Views/Shared/_Layout.cshtml”;
}
朋友
@foreach(模型中的用户项)
{
@项目名称
}
当我调试代码时。我看到我在login方法和login方法中成功地获得了用户模型,然后将三个用户的列表返回到login视图。当我遍历用户记录时,在foreach循环的登录视图中,在foreach循环的第一次迭代中,它在浏览器中显示错误,localhost当前无法处理此请求 我已经在startup类中启用了开发者异常页面,但是没有得到详细的错误信息

Program.cs

public class Program
{
    public static void Main(string[] args)
    {
        CreateHostBuilder(args).Build().Run();
    }

    public static IHostBuilder CreateHostBuilder(string[] args) =>
        Host.CreateDefaultBuilder(args)
            .ConfigureWebHostDefaults(webBuilder =>
            {
                webBuilder.UseStartup<Startup>();
            });
}
公共类程序
{
公共静态void Main(字符串[]args)
{
CreateHostBuilder(args.Build().Run();
}
公共静态IHostBuilder CreateHostBuilder(字符串[]args)=>
Host.CreateDefaultBuilder(args)
.ConfigureWebHostDefaults(webBuilder=>
{
webBuilder.UseStartup();
});
}

按照注释中的说明查看输出窗口后,我发现问题在于在TempData中存储用户对象。我删除了那条线,所有的人都开始工作了。
更新的登录C#代码:

[HttpPost]
公共IActionResult登录(用户)
{
UserProvider UserProvider=新的UserProvider();
User user1=userProvider.CreateUsersList()。其中(x=>x.Username==User.Username&&x.Password==User.Password)。FirstOrDefault();
if(user1==null)
{
ViewData.Clear();
返回视图(“索引”);
}
List users=userProvider.CreateUsersList()。其中(x=>x.Id!=user1.Id).ToList();
返回视图(用户);
}    

在输出窗口中看不到任何异常?System.InvalidOperationException:Microsoft.AspNetCore.Mvc.ViewFeatures.Infrastructure.DefaultTempDataSerializer无法序列化“NewChatApp.Models.User”类型的对象。我在输出窗口中看到这一点,问题是登录方法中的TempData。不确定在TempData中存储用户对象时发生错误的原因。