asp.net core 2 razor pages route with id

asp.net core 2 razor pages route with id

In ASP.NET Core 2.0 Razor Pages, you can define a route with an id parameter in the following way:

  1. Define the id parameter in the page model class:

    public class MyPageModel : PageModel { public int Id { get; set; } // other properties and methods } 
  2. Add a new Razor Page with the name MyPage.cshtml in the Pages folder.

  3. Define the route for the MyPage Razor Page in the Startup.cs file:

    app.UseMvc(routes => { routes.MapRoute( name: "MyPage", template: "MyPage/{id:int}", defaults: new { controller = "MyPage", action = "Index" }); }); 

    This maps the URL pattern MyPage/{id:int} to the MyPage controller and the Index action.

  4. Add a new Index action in the MyPage controller:

    public class MyPageModel : PageModel { public int Id { get; set; } public void OnGet(int id) { Id = id; } } 

    This sets the Id property of the MyPage model to the id parameter passed in the URL.

  5. Create a new Razor Page with the name MyPage.cshtml in the Pages folder:

    @page "{id:int}" <h1>My Page</h1> <p>The ID is @Model.Id</p> 

    This defines the id parameter as a route parameter and displays the Id property of the MyPage model.

With these steps, you should be able to define a route with an id parameter in ASP.NET Core 2.0 Razor Pages. When you navigate to the URL MyPage/{id} in your web browser, the MyPage Razor Page should be displayed with the Id property set to the id parameter in the URL.

Examples

  1. ASP.NET Core 2 Razor Pages Route with ID

    • Description: A general query on how to configure routes with an ID parameter in Razor Pages in ASP.NET Core 2.
    // Code Implementation (In YourPage.cshtml.cs) public class YourPageModel : PageModel { public IActionResult OnGet(int id) { // Use the id parameter in your logic return Page(); } } 
  2. Configuring Route with ID Parameter in ASP.NET Core 2 Razor Pages

    • Description: Guides on configuring a route with an ID parameter in ASP.NET Core 2 Razor Pages.
    // Code Implementation (In YourPage.cshtml.cs) public class YourPageModel : PageModel { public IActionResult OnGet(int id) { // Use the id parameter in your logic return Page(); } } 
  3. ASP.NET Core 2 Razor Pages Custom Route with ID

    • Description: Demonstrates how to create a custom route with an ID parameter in Razor Pages in ASP.NET Core 2.
    // Code Implementation (In YourPage.cshtml.cs) [PageRoute("/custom-route/{id}")] public class YourPageModel : PageModel { public IActionResult OnGet(int id) { // Use the id parameter in your logic return Page(); } } 
  4. ASP.NET Core 2 Razor Pages Route with Optional ID

    • Description: Guides on configuring a route with an optional ID parameter in Razor Pages in ASP.NET Core 2.
    // Code Implementation (In YourPage.cshtml.cs) public class YourPageModel : PageModel { public IActionResult OnGet(int? id) { // Use the id parameter in your logic (null if not provided) return Page(); } } 
  5. Handling ASP.NET Core 2 Razor Pages Route with ID in Form Submissions

    • Description: Illustrates how to handle route parameters, including ID, in form submissions in Razor Pages in ASP.NET Core 2.
    // Code Implementation (In YourPage.cshtml.cs) public class YourPageModel : PageModel { [BindProperty] public YourModel Model { get; set; } public IActionResult OnGet(int id) { // Use the id parameter in your logic return Page(); } public IActionResult OnPost() { // Access Model properties and handle form submission return Page(); } } 
  6. ASP.NET Core 2 Razor Pages Route with Named ID Parameter

    • Description: Demonstrates using a named ID parameter in the route of Razor Pages in ASP.NET Core 2.
    // Code Implementation (In YourPage.cshtml.cs) public class YourPageModel : PageModel { public IActionResult OnGet([FromRoute(Name = "userId")] int id) { // Use the id parameter in your logic return Page(); } } 
  7. Configuring ASP.NET Core 2 Razor Pages Route with GUID ID

    • Description: Guides on configuring a route with a GUID ID parameter in Razor Pages in ASP.NET Core 2.
    // Code Implementation (In YourPage.cshtml.cs) public class YourPageModel : PageModel { public IActionResult OnGet(Guid id) { // Use the id parameter in your logic return Page(); } } 
  8. ASP.NET Core 2 Razor Pages Route with String ID

    • Description: Demonstrates handling routes with a string ID parameter in Razor Pages in ASP.NET Core 2.
    // Code Implementation (In YourPage.cshtml.cs) public class YourPageModel : PageModel { public IActionResult OnGet(string id) { // Use the id parameter in your logic return Page(); } } 
  9. ASP.NET Core 2 Razor Pages Route with Multiple Parameters

    • Description: Guides on configuring routes with multiple parameters, including an ID, in Razor Pages in ASP.NET Core 2.
    // Code Implementation (In YourPage.cshtml.cs) public class YourPageModel : PageModel { public IActionResult OnGet(int id, string category) { // Use the id and category parameters in your logic return Page(); } } 
  10. Handling ASP.NET Core 2 Razor Pages Route with Encoded ID

    • Description: Illustrates handling routes with an encoded ID parameter in Razor Pages in ASP.NET Core 2.
    // Code Implementation (In YourPage.cshtml.cs) public class YourPageModel : PageModel { public IActionResult OnGet([FromRoute] string id) { // Use the id parameter in your logic return Page(); } } 

More Tags

ecmascript-5 custom-taxonomy internationalization select-query multifile-uploader rpgle project-reactor create-table android-design-library leap-year

More C# Questions

More Tax and Salary Calculators

More Biochemistry Calculators

More Pregnancy Calculators

More Gardening and crops Calculators