3
4

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 3 years have passed since last update.

ASP.NET Core (MVC) でPOSTのままリダイレクトする方法

Last updated at Posted at 2021-12-02

ASP.NET Core(MVC)でPOSTしか受け付けないページにリダイレクトすると、

Index.cshtml
<form asp-action="Index" method="post">
    <input type="number" name="number" />
    <button type="submit">SUBMIT</button>
</form>
HomeController.cs
using Microsoft.AspNetCore.Mvc;
using RedirectPostTest.Models;

namespace RedirectPostTest.Controllers
{
    public class HomeController : Controller
    {
        public HomeController()
        {
        }

        [HttpGet]
        public IActionResult Index()
        {
            return View();
        }

        [HttpPost]
        public IActionResult Index(int number)
        {
            if (number == 0)
            {
                return View();
            }
            else
            {
                return RedirectToAction("Hoge", number);
            }
        }

        [HttpPost]
        public IActionResult Hoge(int number)
        {
            return View(new HogeModel(number));
        }
    }
}

こんなふうにエラーが出る(上記のコードで0以外がPOSTされた場合)。

image.png

リダイレクトは基本GETでリクエストが飛ばされる。
元のHTTPメソッド(ここではPOST)を維持してリダイレクトするにはRedirectToActionPreserveMethodメソッドを使う。

HomeController.cs
using Microsoft.AspNetCore.Mvc;
using RedirectPostTest.Models;

namespace RedirectPostTest.Controllers
{
    public class HomeController : Controller
    {
        public HomeController()
        {
        }

        [HttpGet]
        public IActionResult Index()
        {
            return View();
        }

        [HttpPost]
        public IActionResult Index(int number)
        {
            if (number == 0)
            {
                return View();
            }
            else
            {
                //return RedirectToAction("Hoge", number); // こうじゃなくて、
                //return RedirectToActionPreserveMethod(actionName: "Hoge", routeValues: number); // こうでもなくて
                return RedirectToActionPreserveMethod(actionName: "Hoge"); // こう[2021/12/06 修正]
            }
        }

        [HttpPost]
        public IActionResult Hoge(int number)
        {
            return View(new HogeModel(number));
        }
    }
}

RedirectToActionPreserveMethodメソッドは名前付き引数なので書き方に注意する。

[2021/12/06 修正]
routeValues引数は不要だった。
PreserveMethodを付けることでViewに渡すモデルオブジェクトも引き継がれているらしい。
routeValues引数にパラメーターを渡すとURLのクエリ文字列にパラメーターが入ってしまう。


[追記]
HogeModel.csとHoge.cshtmlにも言及しておく。

HogeModel.cs
namespace RedirectPostTest.Models
{
    public class HogeModel
    {
        private int number;

        public HogeModel(int number)
        {
            this.number = number;
        }

        public int Number
        {
            get => number;
            set => number = value;
        }
    }
}
Hoge.cshtml
@model HogeModel

<h2>number : @Model.Number</h2>
3
4
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
3
4

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?