ASP.NET: 错误 - 在'/'应用程序中的服务器错误

4

我正在开发ASP.NET MVC项目,每次尝试运行我的视图Register.cshtml时都会出现以下错误:

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: /User/Register

我正在尝试开发一个注册页面,具体代码如下:
  @{
        ViewBag.Title = "Register";
        Layout = "~/Views/Shared/_Layout.cshtml";
    }

<h2>Register</h2>

<form action='@Url.Action("Register", "Controller")'>

    <input type="hidden" name="FormType" value="A" />
    <input type="hidden" name="FormType" value="B" />
    <input type="text" name="Name" placeholder="enter your name" />
    <input type="text" name="Password" placeholder="enter your name" />
    <input type="radio" name="typeOfForm" class="radioBtn" value="A">Form A
    <input type="radio" name="typeOfForm" class="radioBtn" value="B">Form B
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.7.2/jquery.min.js"></script>
    <div style="display: none" id="formA" action="/actionA">
        <input type="text" name="City" placeholder="City" />  <input type="submit" value="Register Me!" />
    </div>

    <div style="display: none" id="formB" action="/actionB">
        <input type="text" name="Age" placeholder="Age" /><input type="submit" value="Register Me!" />
    </div></form>

<script>
    $('.radioBtn').click(function () {
        var formType = $(this).val();
        //alert(formType);
        if (formType == "A") {
            $('#FormA').show();
            $('#FormB').hide();
        } else {
            $('#FormB').show();
            $('#FormA').hide();
        }
    });</script>

使用UserController.cs

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;

namespace BootstrapSite4.Controllers
{
    public class UserController : Controller
    {   [HttpGet]
        [HttpPost]
        // GET: User
        public ActionResult Register(char FormType, string name, string password)
        {
            Seller S = new Seller();
            DeliveryMan D = new DeliveryMan();
            if (FormType=='A')
                S.Name = name;
            else 
                D.Name = name;
            return View();}}}

我尝试更改我的RegisterRoutes,但仍然收到相同的错误..我认为我的错误只在于位置!关于注册想法的简要说明:我有两种类型的用户将填写注册表格,并根据他们选择的内容(在单选按钮中)其余表格将出现在同一页上,让他们继续注册,然后添加到正确的数据库中。

你定义了哪些路由? - Richard
我已将RegisterRoutes从controller =“Home”,action =“Index”更改为controller =“User”,action =“Register”。 - user5067119
1个回答

4

您需要在UserController中添加一个HttpGet Register方法:

[HttpGet]
public ActionResult Register()
{
    return View();
}

记住,您需要2个注册方法:
  1. GET - 从资源请求数据 - 在这种情况下请求转到注册视图
  2. POST - 提交数据 - 在这种情况下将用户信息发送到服务器,以注册用户
了解更多有关Http Get和Post的信息

1
@user5067119,请你更新控制器代码以便进行更改吗? - Niels Filter
1
谢谢。您已将 HttpGet 添加到相同的方法中。您需要创建一个新的注册方法(这样您就会有两个)。保留原始方法不变,然后在下面添加我指示的方法。 - Niels Filter
1
OP,请确保您理解HTTP动词之间的区别以及它们的工作原理。它们非常重要。 - Inspector Squirrel
1
@user5067119,太好了。你的表单正在提交到错误的控制器。 将 @Url.Action("Register", "Controller") 更改为 @Url.Action("Register", "User") - Niels Filter
@Sippy 是的,我已经添加了一个新的方法,并使用 [HttpGet] 进行了标注。 - user5067119
显示剩余3条评论

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接