使用 asp.net core 的控制器和路由问题

问题描述

我是 asp.net Core 的新手,我偶然发现了路由/控制器的问题。不确定哪一个错误的。总的来说,我不知道如何为多个 .cshtml 文件设置多个控制器,这将允许我在页面之间移动或执行简单的 CRUD 操作。我已经创建了标准的 Web 应用程序项目。我的项目架构看起来像之前的图片

RegisterViewController(应该与 RegisterView 一起使用):

itemData

MainController(应该和Index一起工作,主要是处理移动和从数据库加载少量数据):

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


    }
}

注册View.csHTML代码

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

    }
}

启动路由部分(ofc添加了services.AddMvc();):

@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>

当我试图将该部分更改为:

        {
            if (env.IsDevelopment())
            {
                app.UseDeveloperExceptionPage();
            }


            app.UseHttpsRedirection();
            app.UseStaticFiles();

            app.UseRouting();

            app.UseAuthorization();

            app.UseEndpoints(endpoints =>
            {
                endpoints.MapRazorPages();
            });
        }

我收到错误: [![错误画面][1]][1]

我的 [![Project Schema][2]][2] 也可以在这个截图上找到。

基本上我的代码中有 0 个错误,只是它不起作用。看起来控制器根本不起作用,或者更确切地说它们不被文件使用。我完全熟悉路由和其他东西,所以我知道如何修复它,以便我可以使这些控制器工作并为另一个 cshtml 文件添加新控制器。

提前致谢,对这个问题深表歉意。如果需要其他信息,我会尽快提供。希望可以有人帮帮我。我有点笨,所以如果你能在可能的情况下逐步解释修复,我将不胜感激。

@编辑 总的来说,我的问题是控制器不工作,或者更确切地说,即使我分配了操作,它也根本不工作,如下所示。

家庭控制器:

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

_Layout 片段代码,我在其中调用登录操作( 之上没有任何内容

namespace QSwapper__Aplikacja_do_wymiany_rzeczy.Controllers
{
    public class HomeController : Controller
    {
        public IActionResult Index()
        {
            return View();
        }

        public IActionResult SignIn()
        {
            return View("/Forms/LoginView");
        }


    }

AND STARTUP 路由使用:

<!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>

@edit2 FormsController- 使用 RegisterView

 {
            if (env.IsDevelopment())
            {
                app.UseDeveloperExceptionPage();
            }


            app.UseHttpsRedirection();
            app.UseStaticFiles();

            app.UseRouting();

            app.UseAuthorization();

            app.UseEndpoints(endpoints =>
            {
                endpoints.MapRazorPages();
            });
        }

解决方法

视图采用动作的名称 所以你需要做的是添加一个动作名称属性

[ActionName("LoginView")]

你的控制器启动配置应该是

services.AddControllersWithViews().AddRazorOptions(options => {
            
            options.ViewLocationFormats.Add("/Views/Forms/{0}.cshtml");

      });

相关问答

Selenium Web驱动程序和Java。元素在(x,y)点处不可单击。其...
Python-如何使用点“。” 访问字典成员?
Java 字符串是不可变的。到底是什么意思?
Java中的“ final”关键字如何工作?(我仍然可以修改对象。...
“loop:”在Java代码中。这是什么,为什么要编译?
java.lang.ClassNotFoundException:sun.jdbc.odbc.JdbcOdbc...