最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

c# - Redirecting To (Main Page) Razor Page without changing Address - Stack Overflow

programmeradmin1浏览0评论

newly learning razor

app.MapGet("/", () => Results.Redirect("/Index"));

redirectings to page Index in Pages Folder. web browser address bar seems with route name like this:

http://localhost:5013/Index

is there a way to redirect to Index Page withot seeing Route Name? something like:

http://localhost:5013

newly learning razor

app.MapGet("/", () => Results.Redirect("/Index"));

redirectings to page Index in Pages Folder. web browser address bar seems with route name like this:

http://localhost:5013/Index

is there a way to redirect to Index Page withot seeing Route Name? something like:

http://localhost:5013
Share Improve this question edited yesterday Brando Zhang 28.1k6 gold badges41 silver badges69 bronze badges asked Feb 7 at 9:50 Fables AliveFables Alive 2,8101 gold badge34 silver badges48 bronze badges 3
  • 1 Why do you want to redirect from / to /? That will cause an infinite loop and therefore a Too Many Redirects error in the browser. FYI http://localhost:5013 and http://localhost:5013/Index both point to the same page in your app. – Mike Brind Commented Feb 7 at 11:16
  • 1 app.MapGet("/", () => Results.Redirect("/Index")) is in Program.cs. without this line is app automatically redirects to Pages/Index.cshtml ? mapget is not a need for default main page? answer : I learned something new today. mapget is not a need for initial page. it automatically loads Pages/Index pages without mapget routing code. – Fables Alive Commented Feb 7 at 11:22
  • 1 Take a look at the basics of routing in Razor Pages: learnrazorpages.com/razor-pages/routing – Mike Brind Commented Feb 7 at 14:50
Add a comment  | 

1 Answer 1

Reset to default 2

Actually, this is related with how you set the route for your page.

For example, if you set the route as below inside the index page, then the default page for the / is the index. There is no need to use mapget again.

@page "/"
@model IndexModel
@{
    ViewData["Title"] = "Home page";
}

<div class="text-center">
    <h1 class="display-4">Welcome</h1>
    <p>Learn about <a href="https://docs.microsoft.com/aspnet/core">building Web apps with ASP.NET Core</a>.</p>
</div>

Also if you want to set some custom route for specific page, you could use AddPageRoute method directly inside the page options.

builder.Services.AddRazorPages().AddRazorPagesOptions(options => {

     options.Conventions.AddPageRoute("/Index", "/test");
 
});
发布评论

评论列表(0)

  1. 暂无评论