RazorPageのページ名を指定したリダイレクトのため、リダイレクト先はアプリケーション内のページのみとなります。
アプリケーション外のページや別ドメインのページにリダイレクトする場合は、
こちらの記事のURLを指定したリダイレクトを使用します。
public IActionResult OnPost()
{
return RedirectToPage("/dest-plain");
}
public IActionResult OnPost()
{
return new RedirectToPageResult("/dest-plain");
}
public IActionResult OnPost()
{
return RedirectToPagePermanent("/dest-plain");
}
public IActionResult OnPost()
{
return new RedirectToPageResult("/dest-plain",null,null, true);
}
@page
@addTagHelper *, Microsoft.AspNetCore.Mvc.TagHelpers
@model RedirectPage.Pages.main_redirect_to_pageModel
@{
}
<html>
<head>
<meta name="viewport" content="width=device-width" />
<title></title>
</head>
<body>
<div>リダイレクトのデモ (RedirectToPage)</div>
<form method="post">
<button>リダイレクト</button>
</form>
</body>
</html>
using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.Mvc.RazorPages;
namespace RedirectPage.Pages
{
public class main_redirect_to_pageModel : PageModel
{
public void OnGet()
{
}
public IActionResult OnPost()
{
return RedirectToPage("/dest-plain");
//return RedirectToPagePermanent("/dest-plain"); // 301 リダイレクトの場合
}
}
}
@page
@model RedirectPage.Pages.dest_plainModel
@{
}
<!DOCTYPE html>
<html>
<head>
<meta name="viewport" content="width=device-width" />
<title>Destination Plain</title>
</head>
<body>
リダイレクト先のページです。
</body>
</html>
OnPost()
メソッドを呼び出します。RedirectToPage()
メソッドを呼び出します。第一引数に遷移するRazorPageの名称を与えます。
今回の例の場合、/Pages/dest-plain.cshtml
ファイルのページが遷移先のため、/dest-plain
を与えます。RedirectToPagePermanent()
メソッドを呼び出します。(アプリケーションルートURL)/main-redirect-to-page
にアクセスします。
下図のページが表示されます。@page
@addTagHelper *, Microsoft.AspNetCore.Mvc.TagHelpers
@model RedirectPage.Pages.main_redirect_to_page_resultModel
@{
}
<html>
<head>
<meta name="viewport" content="width=device-width" />
<title></title>
</head>
<body>
<div>リダイレクトのデモ (RedirectToPageResult)</div>
<form method="post">
<button>リダイレクト</button>
</form>
</body>
</html>
using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.Mvc.RazorPages;
namespace RedirectPage.Pages
{
public class main_redirect_to_page_resultModel : PageModel
{
public void OnGet()
{
}
public IActionResult OnPost()
{
return new RedirectToPageResult("/dest-plain");
//return new RedirectToPageResult("/dest-plain",null,null, true); // 301 リダイレクトの場合
}
}
}
@page
@model RedirectPage.Pages.dest_plainModel
@{
}
<!DOCTYPE html>
<html>
<head>
<meta name="viewport" content="width=device-width" />
<title>Destination Plain</title>
</head>
<body>
リダイレクト先のページです。
</body>
</html>
OnPost()
メソッドを呼び出します。/Pages/dest-plain.cshtml
ファイルのページが遷移先のため、/dest-plain
を与えます。true
を与えます。(アプリケーションルートURL)/main-redirect-to-page-result
にアクセスします。
下図のページが表示されます。