I need to change the default start page on my project and it's not working. I'm using
services.AddMvc().AddRazorPagesOptions(options =>
{
options.Conventions.AddPageRoute("/Login/Index", "");
});
But when the project starts, it doesn't go to the LoginController
.
Update. When I try to change the default Controller to LoginController, it incorporates into the main template without setting the
Layout
at the razor page.
public Startup(IConfiguration configuration)
{
Configuration = configuration;
}
public IConfiguration Configuration { get; }
// This method gets called by the runtime. Use this method to add services to the container.
public void ConfigureServices(IServiceCollection services)
{
services.AddRazorPages().AddRazorRuntimeCompilation();
services.AddControllersWithViews();
}
// 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();
}
else
{
app.UseExceptionHandler("/Home/Error");
// The default HSTS value is 30 days. You may want to change this for production scenarios, see https://aka.ms/aspnetcore-hsts.
app.UseHsts();
}
app.UseHttpsRedirection();
app.UseStaticFiles();
app.UseRouting();
app.UseAuthorization();
app.UseEndpoints(endpoints =>
{
endpoints.MapControllerRoute(
name: "default",
pattern: "{controller=Home}/{action=Login}/{id?}");
});
}
If you want to redirect all users to a specific route, that is resolved through a controller, you have to define that route like this:
app.UseMvc(routes =>
{
routes.MapRoute("default", "{controller=Login}/{action=Index}/{id?}")
});
Please note, that while routing to Razor pages works mostly similar, razor pages do not use controllers and are configured differently.
Documentation: https://docs.microsoft.com/en-us/aspnet/core/mvc/controllers/routing?view=aspnetcore-3.1
If you are using Endpoints with razor, setting a default page can actually be done in multiple ways, to which I will supply a couple. The code you currently have in Startup.cs should work with the methods listed below.
By creating a controller with an ActionResult reflecting the path of your default endpoint, root directory should be now be your default page. (Yes, it's this simple.)
public class HomeController : Controller {
public IActionResult Login() => View();
}
[Route()]
Attribute This method is useful if you have multiple ways of visiting a view. I do not suggest using this unless the page breaks the general rules of your routing.
It is also possible to make this a default root page without supplying any defaults in your endpoint routes.
// Startup.cs
public class Startup {
public void ConfigureServices(IServiceCollection service) {
services.AddControllers().AddNewtonsoftJson(jsonSettings);
services.AddControllersWithViews().AddNewtonsoftJson(jsonSettings);
}
public void Configure(IApplicationBuilder app, IWebHostEnvironment env) {
app.UseRouting();
app.UseEndpoints(endpoints => {
endpoints.MapControllerRoute(
name: "default",
pattern: "{controller}/{action}/{id?}");
endpoints.MapRazorPages();
});
}
}
// HomeController.cs
public class HomeController : Controller {
// Putting any Route attribute will remove the default route of '/Home/Login' unless specified.
//[Route("/Home/Login")]
[Route("/")]
public IActionResult Login() => View();
}
Pages are a newer concept in Razor which focus on classes and attributes in .cshtml files rather than controller/action-based project structures. I won't explain specifics but Microsoft have documentation here and a basic tutorial here .
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.