Steve
Steve

Reputation: 4563

Routing for ASP.NET Core Razor Pages

I have been searching for the way to configure default routing for ASP.NET Core Razor Pages but still no success. This is my code for default routing. Anything else I can do? By the way, this is pure Razor pages without MVC.

 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.AddMvc().SetCompatibilityVersion(CompatibilityVersion.Version_2_2);


            services.AddDbContext<AppDbContext>(options =>
                    options.UseSqlServer(Configuration.GetConnectionString("AppDbContext")));

            services.AddMvc()
            .AddRazorPagesOptions(options =>
            {
                options.RootDirectory = "/Bank";

            });
        }

Upvotes: 0

Views: 4181

Answers (3)

Hameed
Hameed

Reputation: 1625

My understanding of the problem (from the comments section), you want to do the followings:

  1. Add routing to custom razor pages.
  2. Change Login page redirection.

You can do the following to add a custom routing to razor pages:

//This should be in the very end.
services.AddMvc().AddRazorPagesOptions(options =>
{
   //just to respect Microsoft way, it is better to have Pages folder
   //to contains your folders.
   options.RootDirectory = "/CustomFolder/Pages";
}).SetCompatibilityVersion(CompatibilityVersion.Version_2_2);

And to change the login page, you need to do:

  1. Add [Authorize] to the pages you want authorization access. Or follow Microsoft guides

If you have Microsoft scaffold-ed pages for Identity e.g.:

services.AddDefaultIdentity<IdentityUser>()
   .AddEntityFrameworkStores<ApplicationDbContext>();

you need to replace it with your own Identity (unless there is a way to override the defaults). Because the defaults would set the login path to: /Identity/Account/Login.

and after implementing your own Identity, you can set the cookies options.

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

Those steps worked for me. And if you insist of having the default Identity you can do add CookieAuthenticationEvents and then implement your own OnRedirectToLogin.

EDIT: Here is some useful sites:

  1. Razor pages configuration.
  2. Configure ASP.NET Core Identity.
  3. Customising-identity.

Upvotes: 1

Ali Doustkani
Ali Doustkani

Reputation: 808

ASP.NET Core MVC itself uses the Routing middleware. You can use that directly if you don't want to use the MVC middleware.

public void ConfigureServices(IServiceCollection services)
{
    services.AddRouting();
}
public void Configure(IApplicationBuilder app, IHostingEnvironment env)
{
    app.UseRouter(cfg =>
    {
        cfg.MapRoute("default", "segmentA/{segmentB}");
    });
}

Upvotes: 0

Ishwor Khanal
Ishwor Khanal

Reputation: 1390

If you have made changes here

 defaults: new { controller = "Bank", action = "Login" });

you should know what kind of action that you need to define in your controller. Normally keep either index or default and if your requirement is to redirect to login every-time app starts then you can set-up in your action.

For example to redirect login action

// 
// GET: /Account/Login 
 [AllowAnonymous] 
 public ActionResult Login(string returnUrl) 
 { 
     ViewBag.ReturnUrl = returnUrl; 
    return View(); 
  } 

Upvotes: 0

Related Questions