【问题标题】:The Resource cannot be found . ASP.NET MVC无法找到该资源 。 ASP.NET MVC
【发布时间】:2018-09-02 00:06:20
【问题描述】:

找不到资源。

Requested URL: /Home/Home/Signup_User

不过应该​​是/Home/Signup_User 其中 Home 是控制器,Signup_UserHome Controller 中的函数。我检查了拼写是否正确。

我的注册表如下。

<form action="~/Home/Signup_User" method="post" enctype="multipart/form-data">
    @Html.AntiForgeryToken()

    <div class="row">
        <div class="large-6 columns">
            <label>
                Enter Name
                @Html.EditorFor(model => model.username, new { htmlAttributes = new { @class = "form-control" } })
            </label>
        </div>
        <div class="large-6 columns">
            <label>
                Enter Age
                @Html.EditorFor(model => model.age, new { htmlAttributes = new { @class = "form-control" } })
            </label>
        </div>
        <div class="large-6 columns">
            <label>
                Enter Email Address
                @Html.TextBoxFor(model => model.email, new { @class = "large-12", placeholder = "xyz@gmail.com", type = "email" })
            </label>
        </div>
        <div class="large-6 columns">
            <label>
                Enter Password
                @Html.PasswordFor(model => model.password, new { })
            </label>
        </div>
    </div>
    <br />
    <hr />
    <button type="submit" class="button tiny right" style="margin-left:5px;color:white;">Submit</button>
    <a href="#" class="button secondary tiny right close-reveal-modal" style="position:relative;    font-size: 0.6875rem;color:inherit;top:0;right:0;   font-weight: 300;" aria-label="Close">Cancel</a> }
    <a class="close-reveal-modal" aria-label="Close">&#215;</a>
</form>

【问题讨论】:

  • 如果你删除~会发生什么?
  • 删除前导~。删除它并使用Html.BeginForm()(或action = "Url.Action(...)"),以便始终正确生成您的网址
  • 你在使用Areas吗
  • 你为什么不使用 FormExtensions 任何背后的原因

标签: c# html asp.net-mvc asp.net-mvc-4


【解决方案1】:

FormExtensions(带有 Controller 和 ActionName)

@using (Html.BeginForm("Signup_User", "Home", FormMethod.Post))
{

}

FormExtensions(带有 Controller 、 ActionName 和 Area )

@using (Html.BeginForm("Signup_User", "Home", FormMethod.Post, new { area = "AreaName" }))
{

}

【讨论】:

    【解决方案2】:

    尝试替换 action="~/Home/Signup_User" 和 action="@Url.Action("Signup_User", "Home")"

    【讨论】:

      【解决方案3】:

      你只需要添加Controller name slash action name,不需要添加~

      解决方案1参考以下代码:

      <form action="/Home/Signup_User" method="post" enctype="multipart/form-data">
          //Add rest of your code
      </form>
      

      否则,使用 MVC 内置的 HTML 帮助器来呈现表单并将其余代码放入相同的代码中。

      @using (Html.BeginForm("Signup_User", "Home", FormMethod.Post))
      {
        // Add your rest of code
      }
      

      以上代码会生成空标签。

      【讨论】:

        【解决方案4】:

        您似乎在 action URL 中使用相对路径。这就是为什么Home 像这样添加了两次。

        /Home/Home/Signup_User
        

        Asp.net MVC 想出漂亮的Form Extensions。只要你指定Controller Name, Action Method NameArea name 如果有的话。

        例如

        @using (Html.BeginForm("actionName", "controllerName", new {area = "AreaName"}))
        

        你的情况

        ActionName :"Signup_User"
        ControllerName:"Home"
        

        假设您没有定义任何区域。用下面的 sn-p 替换您的代码。它将解决您的问题。

        代码:

        @using (Html.BeginForm("Signup_User", "Home"))
                @Html.AntiForgeryToken()
        
                <div class="row">
                    <div class="large-6 columns">
                        <label>
                            Enter Name
                            @Html.EditorFor(model => model.username, new { htmlAttributes = new { @class = "form-control" } })
                        </label>
                    </div>
                    <div class="large-6 columns">
                        <label>
                            Enter Age
                            @Html.EditorFor(model => model.age, new { htmlAttributes = new { @class = "form-control" } })
                        </label>
                    </div>
                    <div class="large-6 columns">
                        <label>
                            Enter Email Address
                            @Html.TextBoxFor(model => model.email, new { @class = "large-12", placeholder = "xyz@gmail.com", type = "email" })
                        </label>
                    </div>
                    <div class="large-6 columns">
                        <label>
                            Enter Password
                            @Html.PasswordFor(model => model.password, new { })
                        </label>
                    </div>
                </div>
                <br />
                <hr />
                <button type="submit" class="button tiny right" style="margin-left:5px;color:white;">Submit</button>
                <a href="#" class="button secondary tiny right close-reveal-modal" style="position:relative;    font-size: 0.6875rem;color:inherit;top:0;right:0;   font-weight: 300;" aria-label="Close">Cancel</a> }
                <a class="close-reveal-modal" aria-label="Close">&#215;</a>
            </form>
        

        【讨论】:

          【解决方案5】:

          发生这种情况的一个原因是,如果您没有在 Web 项目的属性下设置起始页。这样做:

          右键单击您的 mvc 项目 选择“属性” 选择“网络”选项卡 选择“特定页面” 假设您有一个名为 HomeController 的控制器和一个名为 Index 的操作方法,请在“特定页面”单选按钮对应的文本框中输入“home/index”。

          现在,如果您启动 Web 应用程序,它将带您进入由 HomeController 的 Index 操作方法呈现的视图。

          【讨论】:

            猜你喜欢
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 2014-11-07
            • 1970-01-01
            • 1970-01-01
            • 2012-03-06
            • 1970-01-01
            相关资源
            最近更新 更多