使用 asp.net 核心的控制器和路由问题
Problem with controllers and routing using asp.net core
我是 asp.net Core 的新手,我偶然发现了 routing/Controller 的问题。不确定哪个有问题。总的来说,我不知道如何为多个 .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");
}
}
_调用登录操作的布局片段代码( 之上没有任何内容)
<!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 路由 im 使用:
{
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
视图采用动作的名称
所以你需要做的是添加一个动作名称属性
[ActionName("LoginView")]
你的控制器启动配置应该是
services.AddControllersWithViews().AddRazorOptions(options => {
options.ViewLocationFormats.Add("/Views/Forms/{0}.cshtml");
});
我是 asp.net Core 的新手,我偶然发现了 routing/Controller 的问题。不确定哪个有问题。总的来说,我不知道如何为多个 .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");
}
}
_调用登录操作的布局片段代码( 之上没有任何内容)
<!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 路由 im 使用:
{
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
视图采用动作的名称 所以你需要做的是添加一个动作名称属性
[ActionName("LoginView")]
你的控制器启动配置应该是
services.AddControllersWithViews().AddRazorOptions(options => {
options.ViewLocationFormats.Add("/Views/Forms/{0}.cshtml");
});