RazorPagesで指定したURLにリダイレクトする - ASP.NET Core

RazorPagesで指定したURLにリダイレクトするコードを紹介します。

概要

RazorPagesで指定したURLにリダイレクトする場合は、Redirectメソッドを利用して、IActionResultの戻り値に RedirectResultオブジェクトを設定する方法。 RedirectResultオブジェクトを作成して、IActionResultの戻り値に設定する方法があります。

書式

  public IActionResult OnPost()
  {
    return Redirect("https://www.ipentec.com");
  }
  public IActionResult OnPost()
  {
    return new RedirectResult("https://www.ipentec.com");
  }

書式:301リダイレクトの場合

301リダイレクトする場合は、Redirectメソッドを利用している場合は、RedirectPermanent メソッドに変更します。
RedirectResultオブジェクトを作成する方式の場合は、RedirectResultオブジェクトのコンストラクタの第二引数にtrueを指定します。
  public IActionResult OnPost()
  {
    return RedirectPermanent("https://www.ipentec.com");
  }
  public IActionResult OnPost()
  {
    return new RedirectResult("https://www.ipentec.com",true);
  }

例1:Redirectメソッドを利用

コード

main-redirect.cshtml
@page
@addTagHelper *, Microsoft.AspNetCore.Mvc.TagHelpers
@model RedirectPage.Pages.main_redirectModel
@{
}
<html>
<head>
  <meta name="viewport" content="width=device-width" />
  <title></title>
</head>
<body>
  <div>リダイレクトのデモ</div>
  <form method="post">
    <button>リダイレクト</button>
  </form>
</body>
</html>
main-redirect.cshtml.cs
using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.Mvc.RazorPages;

namespace RedirectPage.Pages
{
  public class main_redirectModel : PageModel
  {
    public void OnGet()
    {
    }

    public IActionResult OnPost()
    {
      return Redirect("https://www.ipentec.com");
      // return Redirect("https://www.ipentec.com");  //301リダイレクトの場合
    }
  }
}

解説

ボタンのクリックでPost処理を実行します。OnPostメソッド内で、Redirectメソッドを呼び出し、Redirectメソッドに与えた文字列のURLにリダイレクトします。

実行結果

プロジェクトを実行し (アプリケーションルートURL)/main-redirect/ URLにWebブラウザでアクセスします。 下図のページが表示されます。[リダイレクト]ボタンをクリックします。


コードに記述したURLにリダイレクトしてページ遷移します。


なお、301リダイレクトのコードを利用した場合は、リダイレクトのコードが301になります。
301リダイレクトの場合
301リダイレクトの場合


例2:RedirectResultオブジェクトを利用

コード

main-redirect-result.cshtml
@page
@addTagHelper *, Microsoft.AspNetCore.Mvc.TagHelpers
@model RedirectPage.Pages.main_redirect_resultModel
@{
}
<html>
<head>
  <meta name="viewport" content="width=device-width" />
  <title></title>
</head>
<body>
  <div>リダイレクトのデモ</div>
  <form method="post">
    <button>リダイレクト</button>
  </form>
</body>
</html>
main-redirect.cshtml.cs
using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.Mvc.RazorPages;

namespace RedirectPage.Pages
{
  public class main_redirect_resultModel : PageModel
  {
    public void OnGet()
    {
    }

    public IActionResult OnPost()
    {
      return new RedirectResult("https://www.ipentec.com");

      // return new RedirectResult("https://www.ipentec.com", true);  //301リダイレクトの場合
    }
  }
}

解説

ボタンのクリックでPost処理を実行します。 OnPostメソッド内で、RedirectResultオブジェクトを作成します。OnPostの戻り値にRedirectResultオブジェクトを返すことでリダイレクトします。

実行結果

プロジェクトを実行し (アプリケーションルートURL)/main-redirect/ URLにWebブラウザでアクセスします。 下図のページが表示されます。[リダイレクト]ボタンをクリックします。


コードに記述したURLにリダイレクトしてページ遷移します。


なお、301リダイレクトのコードを利用した場合は、リダイレクトのコードが301になります。
301リダイレクトの場合
301リダイレクトの場合


著者
iPentecのメインプログラマー
C#, ASP.NET の開発がメイン、少し前まではDelphiを愛用
最終更新日: 2024-03-24
作成日: 2024-03-24
iPentec all rights reserverd.