An unhandled exception occurred while processing the request.
InvalidOperationException: No page named '/results/destination' matches the supplied values.
@page
@model RedirectPage.Pages.wrong_demo.IndexModel
@addTagHelper *, Microsoft.AspNetCore.Mvc.TagHelpers
@{
}
<html>
<head>
</head>
<body>
<p>リダイレクトのテストページ</p>
<form method="post">
<input type="submit" value="POST"/>
</form>
</body>
</html>
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.Mvc.RazorPages;
namespace RedirectPage.Pages.wrong_demo
{
public class IndexModel : PageModel
{
public void OnGet()
{
}
public IActionResult OnPost()
{
return RedirectToPage("/results/destination");
}
}
}
@page "/results/destination"
@{
}
<html>
<head></head>
<body>
<p>リダイレクト先のページです。</p>
</body>
</html>
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Hosting;
using Microsoft.AspNetCore.Http;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Hosting;
namespace RedirectPage
{
public class Startup
{
// This method gets called by the runtime. Use this method to add services to the container.
// For more information on how to configure your application, visit https://go.microsoft.com/fwlink/?LinkID=398940
public void ConfigureServices(IServiceCollection services)
{
services.AddRazorPages();
}
// This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
{
if (env.IsDevelopment()) {
app.UseDeveloperExceptionPage();
}
app.UseRouting();
app.UseEndpoints(endpoints =>
{
endpoints.MapRazorPages();
});
}
}
}
@page "/results/destination"
が指定されているため、このページのURLは (アプリケーションルート)/resutls/destination
のURLになります。(アプリケーションルート)/demo-pagename/main
を開きます。ページが表示され、サブミットボタンが表示されています。(アプリケーションルート)/demo-pagename/dest
を開きます。こちらのページは page ディレクティブで別の名称が設定されているため表示できません。(アプリケーションルート)/results/destination
を開きます。ページが表示されることが確認できました。(アプリケーションルート)/demo-pagename/main
に戻りフォーム内のサブミットボタン([]ボタン)をクリックします。RedirectToPage
メソッドの第一引数 pageName にはページのリダイレクトURLではなくページのファイル名を指定する必要があります。/Pages/demo-pagename/dest.cshtml
に配置されているファイルのため、"/demo-pagename/dest" を与える必要があります。using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.Mvc.RazorPages;
namespace RedirectPage.Pages.wrong_demo
{
public class IndexModel : PageModel
{
public void OnGet()
{
}
public IActionResult OnPost()
{
return RedirectToPage("/demo-pagename/dest");
}
}
}
(アプリケーションルート)/demo-pagename/main
を開きます。ページが表示され、サブミットボタンが表示されます。(アプリケーションルート)/results/destination
になっており、dest.cshtml に設定した @page ディレクティブで指定したURLに
正しくリダイレクトされます。using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.Mvc.RazorPages;
namespace RedirectPage.Pages.wrong_demo
{
public class IndexModel : PageModel
{
public void OnGet()
{
}
public IActionResult OnPost()
{
return Redirect("/results/destination");
}
}
}
(アプリケーションルート)/demo-pagename/main
を開きます。ページが表示され、サブミットボタンが表示されます。