1
votes

I'm working on a ASP.Net Core 2.1 site with Razor pages, first time I've used Razor pages. But what I'd like to do is change the home or landing page. So if a user is not logged in The site should redirect to the /Account/Login page in the Areas folder, but if the user is logged in it should go to a page called DataManagement as shown below in the pages folder.

enter image description here

I've already got the Identity sewn in and I've tried something like below in Configure Services :

services.AddMvc().AddRazorPagesOptions(options =>
{
    options.Conventions.AddAreaPageRoute("Identity", "/Account/Login", "");
}).SetCompatibilityVersion(CompatibilityVersion.Version_2_1);

And in the configure method :

app.UseMvc(routes =>
{
    routes.MapRoute(
        name: "default",
        template: "{controller=Home}/{action=Index}/{id?}");
});

To no avail.

EDIT My StartUp.cs

public class Startup { 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.Configure<CookiePolicyOptions>(options =>
        {
            // This lambda determines whether user consent for non-essential cookies is needed for a given request.
            options.CheckConsentNeeded = context => true;
            options.MinimumSameSitePolicy = SameSiteMode.None;
        });

        services.AddDbContext<ApplicationDbContext>(options =>
            options.UseSqlServer(
                Configuration.GetConnectionString("Connection")));
        services.AddIdentity<ApplicationUser, ApplicationRole>(
            options => options.Stores.MaxLengthForKeys = 128)
            .AddEntityFrameworkStores<ApplicationDbContext>()
            .AddDefaultUI()
            .AddDefaultTokenProviders()
            .AddEntityFrameworkStores<ApplicationDbContext>();


        services.AddMvc().AddRazorPagesOptions(opts =>
        {
            opts.Conventions.AddPageRoute("/DataManagement", "/");
            opts.Conventions.AddPageRoute("/DataManagement", "home");
            opts.Conventions.AddPageRoute("/DataManagement", "index");

        }).SetCompatibilityVersion(CompatibilityVersion.Version_2_1);


    }

    // This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
    public void Configure(IApplicationBuilder app, IHostingEnvironment env, ApplicationDbContext context, RoleManager<ApplicationRole> roleManager, UserManager<ApplicationUser> userManager, IServiceProvider serviceProvider)
    {
        if (env.IsDevelopment())
        {
            app.UseDeveloperExceptionPage();
            app.UseDatabaseErrorPage();
        }
        else
        {
            app.UseExceptionHandler("/Error");
            app.UseHsts();
        }

        app.UseHttpsRedirection();
        app.UseStaticFiles();
        app.UseCookiePolicy();

        app.UseAuthentication();

        app.UseMvc();

    }
1
you should change route config. your template is configured to if controller name is not specified in route default controller is Home and if action is not specified in the route default action is Index. You have two option to handle this. First one is change default controller or Index or change redirection route.Baris
Hey @Baris could you give me a URL or example for that?yamamoto585

1 Answers

2
votes

The simplest way to do this would just to use the [Authorize] attribute on your DataManagement.cshtml.cs file.

[Authorize]
public class DataManagementModel : PageModel
{
    public void OnGet()
    {

    }
}

Just configure your default Homepage as usual in Startup.cs:

services.AddMvc().AddRazorPagesOptions(opts =>
{
    opts.Conventions.AddPageRoute("/DataManagement", "/");
    opts.Conventions.AddPageRoute("/DataManagement", "home");
    opts.Conventions.AddPageRoute("/DataManagement", "index");
    opts.Conventions.AddAreaPageRoute("Account", "/Login", "/Account/Login");

}).SetCompatibilityVersion(CompatibilityVersion.Version_2_1);

services.ConfigureApplicationCookie(options =>
{
    options.LoginPath = $"/Account/Login";
});

and in Configure :

        app.UseMvc(routes =>
        {
            routes.MapRoute(
               name: "default",
               template: "{controller=Home}/{action=Index}/{id?}");

        });

Then remove the Index.cshtml

I think in this case you need to define custom login as above, taken from here