【问题标题】:ASP.NET : Error - Server Error in '/' ApplicationASP.NET:错误 - “/”应用程序中的服务器错误
【发布时间】:2015-10-09 14:28:24
【问题描述】:

我正在处理 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 但仍然收到相同的错误 .. 我认为我的错误仅与位置有关!只是关于注册想法的简要描述:我有两种类型的用户将填写注册表格,根据他们选择的内容(在单选按钮中),表格的其余部分将出现在同一页面中,让他们继续注册将其添加到正确的数据库中。

【问题讨论】:

  • 你定义了哪些路由?
  • 我已将 RegisterRoutes 从 controller = "Home", action = "Index" 更改为 controller = "User", action = "Register"

标签: c# asp.net asp.net-mvc registration


【解决方案1】:

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

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

记住你需要2个注册方法:

  1. GET - 从资源请求数据 - 在这种情况下请求转到注册视图
  2. POST - 提交数据 - 在这种情况下,将用户信息发送到服务器,注册用户

More reading on Http Get and Post

【讨论】:

  • @user5067119,你能用更改更新控制器代码吗?
  • 谢谢。您已将 HttpGet 添加到 same 方法中。您需要创建一个 new 注册方法(因此您将拥有 2 个)。保持原样,然后在其下方添加我在答案中指出的那个。
  • OP,请确保您了解 HTTP 动词之间的区别以及它们的工作方式。它们很重要。
  • 非常感谢它的工作!但每当我按下“注册我!”按钮我得到同样的错误!
  • @user5067119,太好了。您的表单提交给了错误的控制器。将@Url.Action("Register", "Controller") 更改为@Url.Action("Register", "User")
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-09-18
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多