Routing for ASP.NET Core Razor Pages

razor pages redirect to page with parameter
asp net core razor pages master detail
asp net core razor pages with web api
razor pages route constraints
razor pages routing multiple parameters
razor pages onget parameters
razor pages change default page
razor pages get current page name

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";

            });
        }

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.

Razor Pages route and app conventions in ASP.NET Core , Route Assumptions. Routing in Razor Pages relies on a primary assumption: unless you tell ASP.NET Core 3.0 otherwise, the folder and page  Route Assumptions. Routing in Razor Pages relies on a primary assumption: unless you tell ASP.NET Core 3.0 otherwise, the folder and page name of any given Razor Page will be used to make the route to that page. For example, say we have a Razor Page located at /Testing/MockTests.cshtml.

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(); 
  } 

Routing in asp net core razor pages, supports cross platform development and can be deployed to Windows, Unix and Mac operating systems. For Razor Pages conventions, see Razor Pages route and app conventions in ASP.NET Core. The endpoint routing system described in this document applies to ASP.NET Core 3.0 and later. For information on the previous routing system based on IRouter, select the ASP.NET Core 2.1 version using one of the following approaches:

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}");
    });
}

How to Use Routing in ASP.NET Core 3.0 Razor Pages, has been all the rage recently for web applications across most programming languages. Razor Pages Routing Routing is the system that matches URLs to Razor pages. Like most page-centric frameworks, the primary routing system in ASP.NET Razor Pages is based on matching URLs to file paths, starting from the root Razor Pages folder, which is named Pages by default. How URLs are matched link

Learn Razor Pages: An Introduction To ASP.NET Core Razor Pages, Routing in Razor Pages. 11 July 2017 13:28. ASP.NET Core Razor Pages. One of the top Rule number one is that Razor Pages need a root folder. By default  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.

Routing in ASP.NET Core MVC, Each route (each pair of actions) becomes a separate Razor Page, instead of grouping many similar actions together under a single controller. ASP.NET Core Razor Pages One of the top level design considerations for the developers of a server-side web application framework is how to match URLs to resources on the server so that the correct one processes the request.

ASP.NET Razor Pages vs MVC: Benefits and Code Comparisons, If you need to be able to cater for either a referenceid or a clientid , or both, you can either make the first parameter required and pass in 0,  Razor Pages works on top of ASP.NET Core and has many of the same features found in ASP.NET MVC: Routing, Model Binding, ModelState, Validation, Razor views, and ActionResult return types. We see the most significant differences in the supported HTTP semantics of Razor Pages.

Comments
  • What exactly do you want to know, that isn't covered by the docs? docs.microsoft.com/de-de/aspnet/core/razor-pages/…
  • Does AddRazorPagesOptions cover your needs? Take a look at docs.microsoft.com/en-us/aspnet/core/razor-pages/…
  • i tried also but doesn't work. updated code in original post.
  • at the monent, i can only login using full path, localhost:53830/Bank/Login. I wish to just type localhost:53830 to route me to localhost:53830/Bank/Login
  • To specify that your Razor Pages are at a custom root directory in the app, you need to add .WithRazorPagesRoot("/path/to/razor/pages"); after your AddRazorPagesOptions. And I think you can use ConfigureApplicationCookie to specify the LoginRedirection. Also you need to decorate your page with [Authorize]
  • thanks for this info. actually, at the moment, i am just using old school session. haven't implement asp.net core identity.
  • I tried your code. I think it does work half way. Now i am to figure out where to set my login page. services.AddMvc().AddRazorPagesOptions(options => { options.RootDirectory = "/Pages/Bank/"; }).SetCompatibilityVersion(CompatibilityVersion.Version_2_2);
  • AmbiguousMatchException: The request matched multiple endpoints. Matches: Page: /Index Page: //Index
  • That's probably because you have a page inside pages folder that has the same name as a folder inside the pages folder with Index.cshtml.
  • If you want auto login, sure use Authorize attributes and setup authentication middleware?