首页 > 解决方案 > 路由 ASP.Net MVC 的问题

问题描述

当我点击一个类后使用 ISS 运行应用程序时。浏览器使用此 url 启动。http://localhost:50282/

当我在我的文件夹 Account In Views 中单击 Index 并运行应用程序时,我得到这个 url:http://localhost:50282/Account/Index

现在,在这两个 url 上,我都有一个注册表单,它链接到我的 AccountController 中的一个操作。当我在第二种情况下提交表单时,我得到这个 url: http://localhost:50282/Account/register并且 register 方法在我的 AccountController 类中运行并且工作正常。

在第一种情况下,我得到这个 url 和错误:

网址:http://localhost:50282/register
错误:“/”应用程序中的 404
服务器错误。
无法找到该资源。
说明:HTTP 404。您要查找的资源(或其依赖项之一)可能已被删除、名称已更改或暂时不可用。请查看以下 URL 并确保其拼写正确。

请求的 URL:/注册

我希望 url 在单击注册后转到第二个 url http://localhost:50282/Account/register ,无论我在运行应用程序之前单击了哪里。

提交表单查看代码:

    @{
        ViewBag.Title = "Index";
    }
    <h1>Register Form </h1>

    <form action="register" method="post">
    <label><i class="" aria-hidden="true"></i> Username </label>
    <input type="text" name="Username" placeholder="Enter User Name"     required="" />
    <br>
    <label><i class="" aria-hidden="true"></i> password </label>
    <input type="password" name="Password" placeholder="Enter Password" required="" id="myInput" />

    <input type="submit" value="Register">
</form>

标签: asp.netasp.net-mvc

解决方案


您需要指定表单的提交位置。所以改变这个...

<form action="register" method="post"> 
    <label>
    <i class="" aria-hidden="true"></i> Username </label> 
    <input type="text" name="Username" placeholder="Enter User Name" required="" /> 
    <br> 
    <label>
    <i class="" aria-hidden="true"></i> password </label> 
    <input type="password" name="Password" placeholder="Enter Password" required="" id="myInput" /> <input type="submit" value="Register"> 
</form>

到您的表单的 html 助手...

@using(Html.BeginForm("Register", "Account"))
{

 <label><i class="" aria-hidden="true"></i> Username </label> 
 <input type="text" name="Username" placeholder="Enter User Name" required="" /> 
 <br> 
 <label><i class="" aria-hidden="true"></i> password </label> 
 <input type="password" name="Password" placeholder="Enter Password" required="" id="myInput" /> 
 <input type="submit" value="Register"> 
 }

您还需要添加antiforgery token安全性 ( @html.Antiforgerytoken())。并用属性装饰Register动作。[ValidateAntiforgeryToken]

看到这个


推荐阅读