6

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");
}
3
  • 1
    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. Commented Sep 7, 2023 at 8:36
  • @QingGuo thanks for reply . question updated please check Commented Sep 7, 2023 at 8:51
  • Have a look at my updated answer. Commented Sep 7, 2023 at 9:10

1 Answer 1

11

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

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.