【问题标题】:MVC4 RedirectToAction returns HTTP 404MVC4 RedirectToAction 返回 HTTP 404
【发布时间】:2013-11-10 11:12:29
【问题描述】:

我的 RedirectToAction 返回 HTTP 404,除了控制器、操作和视图都在那里,拼写正确并带有适当的大写字母。我在 SubscriberController 中,我想在 CreateTestController 中调用一个操作。

这是我的初始网址:

http://www.localdomain.com/Subscriber/AuthUser

这会启动其中包含多个表单的页面,我用于此调用的表单如下:

<form id="btnform7" action="LaunchTestPortal" method="post">
    <input name="__RequestVerificationToken" id="antiforge7" type="hidden" value="rzTdAi...aj501">
    <button title="Create, Edit or Review Training" class="ui...active" id="btnLaunchTestPortal" role="button" style="width: 13em; height: 22px; margin-bottom: 10px;" type="submit">
        <span class="ui-text-only">Create, Edit or Review Training</span>
    </button>
</form>

这成功调用了SubscriberController中的这个方法

[HttpPost]
[ValidateAntiForgeryToken]
public ActionResult LaunchTestPortal()
{
    return RedirectToAction("CreateTestPortal", "CreateTest");
}

这是我要在 CreateTestController 中调用的方法

[HttpPost]
[ValidateAntiForgeryToken]
public ViewResult CreateTestPortal()
{
    ...
}

调用后,这是它返回的地址http://www.localdomain.com/CreateTest/CreateTestPortal

除了页面结果是 HTTP 404 not found。 所有的零件都在那里。它只是控制器A中的重定向不会调用控制器B中的方法

【问题讨论】:

  • 我觉得应该是RedirectToAction("CreateTestPortal")
  • 不,如果我这样做,它会尝试将其发送到“Subscriber/CreateTestPortal”,但我需要将其发送到“CreateTest/CreateTestPortal”
  • 澄清一下,如果你直接 POST 到localdomain.com/CreateTest/CreateTestPortal,它也会失败吗?这表明您遇到了路由问题。
  • 是的,当我将表单直接发布到 localdomain/CreateTest/CreateTestPortal 时,它会找到并正确启动它。

标签: asp.net-mvc


【解决方案1】:

我把cmets放在了我的修改里,方便大家学习。

public class SubscriberController : Controller
{
    //
    // GET: /Subscriber/
    public ActionResult AuthUser()
    {
        return View();
    }

    [HttpPost]
    [ValidateAntiForgeryToken]
    public ActionResult LaunchTestPortal()
    {
        return RedirectToAction("CreateTestPortal", "CreateTest");
    }
}

public class CreateTestController : Controller
{
    //removed two attributes
    public ViewResult CreateTestPortal()
    {
        var ap = "dh";
        return View();
    }
}

AuthUser.cshtml

<!DOCTYPE html>

<html>
<head>
    <meta name="viewport" content="width=device-width" />
    <title>AuthUser</title>
</head>
<body>
    @*added Subscriber below*@
    <form id="btnform7" action="/Subscriber/LaunchTestPortal" method="post">
        @*changing next line*@
        @Html.AntiForgeryToken()
        @*<input name="__RequestVerificationToken" id="antiforge7" type="hidden" value="rzTdAi...aj501">*@
        <button title="Create, Edit or Review Training" class="ui...active" id="btnLaunchTestPortal" role="button" style="width: 13em; height: 22px; margin-bottom: 10px;" type="submit">
            <span class="ui-text-only">Create, Edit or Review Training</span>
        </button>
    </form>
</body>
</html>

CreateTestPortal.cshtml

<html>
<head>
    <meta name="viewport" content="width=device-width" />
    <title>CreateTestPortal</title>
</head>
<body>
    <div> 
    </div>
</body>
</html>

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2022-11-11
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多