【发布时间】:2021-04-21 16:29:23
【问题描述】:
我是 asp.net Core 的新手,我偶然发现了我的路由/控制器的问题。不确定是哪一个有问题。总的来说,我不知道如何为多个 .cshtml 文件设置多个控制器,这将允许我在页面之间移动或执行简单的 CRUD 操作。我已经创建了标准的 Web 应用程序项目。我的项目架构看起来像之前的图片。
RegisterViewController(应该与 RegisterView 一起使用):
using Microsoft.AspNetCore.Mvc;
using QSwapper__Aplikacja_do_wymiany_rzeczy.Models;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
namespace QSwapper__Aplikacja_do_wymiany_rzeczy.Controllers
{
[Route("Forms")]
public class RegisterViewController : Controller
{
private QSwapperDataContext db = new QSwapperDataContext();
[Route("")]
[Route("LoginView")]
[Route("~/")]
public IActionResult Index()
{
return View();
}
[HttpGet]
[Route("RegisterView")]
public IActionResult RegisterView(UserInfo userinfo)
{
db.UserInfos.Add(userinfo);
db.SaveChanges();
return RedirectToAction("Index");
}
}
}
MainController(应该和Index一起工作,主要是处理移动和从数据库加载少量数据):
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
namespace QSwapper__Aplikacja_do_wymiany_rzeczy.Controllers
{
public class HomeController : Controller
{
public IActionResult Index()
{
return View();
}
}
}
RegisterView.cshtml 代码:
@page
@model App.Models.UserInfo
@{
ViewData["Title"] = "Sign up";
Layout = null;
}
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>Rejestracja</title>
<meta name="description" content="Register">
<link rel="stylesheet" href="/css/Register.css">
</head>
<body>
<div class="blurred-box">
<div class="user-login-box">
<span class="user-icon"></span>
<div class="user-name">
<form method="post" asp-controller="RegisterView" asp-action="RegisterView">
<div class="section">
<input class="user-register" type="text" placeholder="Login" asp-for="UserLogin"/>
<input class="user-register" type="password" placeholder="Password" asp-for="UserPassword"/>
<input class="user-register" type="text" placeholder="Name" asp-for="UserName"/>
<input class="user-register" type="text" placeholder="Surname" asp-for="UserSurname"/>
<input class="user-register" type="email" placeholder="email" asp-for="UserEmail"/>
</div>
<div class="section">
<input class="user-register" type="text" placeholder="adress" asp-for="UserAdress" />
<input class="user-register" type="text" placeholder="city" asp-for="UserCity" />
<input class="user-register" type="text" placeholder="PostalCode" asp-for="UserPostalCode" />
<input class="user-register" type="text" placeholder="Contact Number" asp-for="UserContact" />
</div>
</form>
</div>
<input class="user-button" type="submit" value=">"/>
</div>
</div>
</body>
</html>
启动路由部分(ofc添加的services.AddMvc();):
{
if (env.IsDevelopment())
{
app.UseDeveloperExceptionPage();
}
app.UseHttpsRedirection();
app.UseStaticFiles();
app.UseRouting();
app.UseAuthorization();
app.UseEndpoints(endpoints =>
{
endpoints.MapRazorPages();
});
}
当我试图将那部分更改为:
public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
{
if (env.IsDevelopment())
{
app.UseDeveloperExceptionPage();
}
app.UseStaticFiles();
app.UseRouting();
app.UseCors();
app.UseEndpoints(endpoints =>
{
endpoints.MapControllerRoute("default", "{controller=UserInfo}/{action=Index}");
});
}
我收到错误: [![错误画面][1]][1]
我的 [![Project Schema][2]][2] 也可以在此屏幕截图中找到。
基本上我的代码中有 0 个错误,只是它不起作用。看起来控制器根本不起作用,或者更确切地说它们没有被文件使用。我对路由和其他东西非常满意,所以我知道如何修复它,这样我就可以使这些控制器工作并为另一个 cshtml 文件添加新控制器。
提前致谢,对问题深表歉意。如果需要其他信息,我会尽快提供。希望可以有人帮帮我。我有点笨,所以如果你能在可能的情况下逐步解释修复,我将不胜感激。
@编辑 总的来说,我的问题是控制器无法正常工作,或者即使我分配了操作,它也根本无法正常工作,如下所示。
家庭控制器:
namespace QSwapper__Aplikacja_do_wymiany_rzeczy.Controllers
{
public class HomeController : Controller
{
public IActionResult Index()
{
return View();
}
public IActionResult SignIn()
{
return View("/Forms/LoginView");
}
}
_Layout 片段代码,我在其中调用登录操作( 上方没有任何内容)
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<link rel="stylesheet" href="~/lib/bootstrap/dist/css/bootstrap.min.css" />
<link rel="stylesheet" href="/css/site.css" />
</head>
<body>
<header>
<div id="mainWrapper">
<!-- Contains Logo and links -->
<div id="logo">
<img src="file:///Users/trishaolalia/Desktop/hanah_olalia_site/shoe-logo.pdf" alt="sample logo">
</div>
<div id="headerLinks">
<input type="text" id="search" value="search">
<a asp-controller="Home" asp-action="" title="Login">Logowanie</a>
<a href="/Forms/RegisterView" title="Register">Rejestracja</a>
</div>
AND STARTUP 路由即时使用:
{
if (env.IsDevelopment())
{
app.UseDeveloperExceptionPage();
}
app.UseHttpsRedirection();
app.UseStaticFiles();
app.UseRouting();
app.UseAuthorization();
app.UseEndpoints(endpoints =>
{
endpoints.MapRazorPages();
});
}
@edit2 FormsController- 使用 RegisterView
{
private QSwapperDataContext db = new QSwapperDataContext();
[HttpGet]
public IActionResult RegisterView()
{
return View();
}
[HttpPost]
public IActionResult RegisterView(UserInfo userinfo)
{
db.UserInfos.Add(userinfo);
db.SaveChanges();
return RedirectToAction("Index");
}
}
}```
as for .cshtml code for RegisterView it stays the same. Overall there's no action at all. It doesn't save to database or redirect to Index Page
[1]: https://i.stack.imgur.com/C3J30.png
[2]: https://i.stack.imgur.com/l3hg1.png
【问题讨论】:
-
浏览器中的网址是什么?
-
return View()在RegisterViewController的索引操作中在 RegisterView 文件夹中查找名称为 Index 的视图。如果找不到,它将在共享文件夹中查找。在您的情况下,这两个位置都不存在 Index.cshtml。这就是您收到此错误的原因。 -
@ChetanRanpariya 好的。我明白这一点,但即使在 HomeController 中,当我编写简单的操作以移动到另一个视图时也是如此。 ``` public IActionResult SignIn() { return View(/Forms/LoginView); }``` 和 ofc 在我的 HTML 中使用了 asp-controll="Home" asp-action="SingIn" 它没有移动到另一个视图。看起来像返回索引。
-
你的意思是
return View(/Forms/LoginView);返回了家庭控制器的索引视图?你能用最新的代码更新问题吗? -
@ChetanRanpariya 完成。在〜编辑下面添加了这些部分。非常感谢您的帮助
标签: c# asp.net asp.net-mvc asp.net-mvc-routing routing-controllers