Home > Software engineering >  Redirect from _Layout.cshtml to homepage on page load
Redirect from _Layout.cshtml to homepage on page load

Time:04-05

I want to load to my homepage when I start, so that the _Layout page doesn't show and my website starts at the homepage. I have tried to redirect the page to the homepage but due to it being a template for the other pages it is in a constant loop of redirects.

@{ Context.Response.Redirect("/Homepage");}

How can I fix my issue and be able to go to my homepage while skipping the _layout page when I start the server.

CodePudding user response:

If you are trying to have a different page by default you have set your routes in the startup to have a default

app.MapControllerRoute(
    name: "default",
    pattern: "{controller=Home}/{action=Index}/{id?}");

And you can change the pattern for the controller and action there that would load by default if those parameters are missing. Note this will work only if not other routing options are present, either attribute routing or something else.

So perhaps you want something like this depending on the controller?

app.MapControllerRoute(
    name: "default",
    pattern: "{controller=Home}/{action=Homepage}/{id?}");

Edit:

If you are using razor pages, it can get trickier and the simpliest solution is to just use attribute routing.

At the top of the page you want to be the default, put

@page "/"

And for the previous default page put whatever you like, something like

@page "/NotDefaultAnymore"

Or whatever you would like the route to be.

  • Related