Using razor pages in ASP.NET Core minimal API
Asked Answered
A

1

5

I am using Minimal API in ASP.NET Core. I need to return a Razor page. Is there any way to do this?

I have added

builder.Services.AddRazorPages();
app.MapRazorPages();

in program.cs file. And I have index.cshtml file under the root Page folder, but it doesn't work:

public async static Task<RedirectToPageResult> Get()
{
    return new RedirectToPageResult("index");
}
Anabas answered 7/9, 2023 at 8:30 Comment(3)
What do you mean return razor page ? Do you want to add two more calls - AddRazorPages() and MapRazorPages() - for supporting Razor Pages ? If so, you can add Pages folder under the project root. And add a new Razor Page named Index.cshtml to it.Demoss
@QingGuo thanks for reply . question updated please checkAnabas
Have a look at my updated answer.Demoss
D
10

Try the below steps:

In Program.cs add two more calls - AddRazorPages() and MapRazorPages() - for supporting Razor pages :

var builder = WebApplication.CreateBuilder(args);

builder.Services.AddRazorPages();

var app = builder.Build();

app.MapGet("/", () => "Hello World!");
app.MapRazorPages();
app.Run();

Add Pages folder under the project root. And add a new Razor page named Index.cshtml to it, like this:

enter image description here

Result:

enter image description here

Update

Create a Student.cshtml razor page then try:

 public async Task<RedirectToPageResult> OnGetAsync()
 {
     return new RedirectToPageResult("Student");
 }

Result:

enter image description here

Demoss answered 7/9, 2023 at 8:53 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.