{"id":1969,"date":"2024-10-31T17:10:02","date_gmt":"2024-10-31T15:10:02","guid":{"rendered":"https:\/\/epicmarketing.co.il\/notebook\/?p=1969"},"modified":"2024-10-31T17:14:38","modified_gmt":"2024-10-31T15:14:38","slug":"routing-in-asp-net-core","status":"publish","type":"post","link":"https:\/\/epicmarketing.co.il\/notebook\/routing-in-asp-net-core\/","title":{"rendered":"Routing in ASP.NET Core"},"content":{"rendered":"\n<p class=\"wp-block-paragraph\"><strong>What is Routing?<\/strong> Routing in ASP.NET Core is the process of mapping an incoming HTTP request to a particular <strong>controller action<\/strong>. When a request is made to an API, routing helps to determine which action should handle it based on the URL and other request parameters.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">There are two main types of routing:<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li><strong>Attribute Routing<\/strong>: Defined by placing attributes directly on controllers and actions.<\/li>\n\n\n\n<li><strong>Conventional Routing<\/strong>: Defined centrally in the <code>Program.cs<\/code> file using a pattern.<\/li>\n<\/ul>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>1. IActionResult and How to Use It<\/strong><\/h2>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>What is <code>IActionResult<\/code>?<\/strong> <code>IActionResult<\/code> is an interface in ASP.NET Core that represents a result of an action method in a controller. When you define an action in your Web API controller, you often need to specify how the response will be returned to the client.<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li><code>IActionResult<\/code> provides flexibility, allowing you to return different types of HTTP responses (e.g., <code>Ok<\/code>, <code>BadRequest<\/code>, <code>NotFound<\/code>, etc.).<\/li>\n\n\n\n<li>It is very useful when you need to <strong>return different response types<\/strong> based on conditions (e.g., success vs. failure).<\/li>\n<\/ul>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>Examples of <code>IActionResult<\/code> in Use<\/strong>:<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: csharp; title: ; notranslate\" title=\"\">\n&#x5B;HttpGet(&quot;product\/{id}&quot;)]\npublic IActionResult GetProduct(int id)\n{\n    if (id &lt;= 0)\n    {\n        return BadRequest(&quot;Invalid product ID.&quot;);\n    }\n\n    var product = new Product { Id = id, Name = &quot;Sample Product&quot;, Price = 10.0m };\n    if (product == null)\n    {\n        return NotFound();\n    }\n\n    return Ok(product);\n}\n<\/pre><\/div>\n\n\n<ul class=\"wp-block-list\">\n<li><strong><code>Ok(object value)<\/code><\/strong>: Returns a status code of <strong>200 OK<\/strong> with the specified data.<\/li>\n\n\n\n<li><strong><code>BadRequest(string message)<\/code><\/strong>: Returns a <strong>400 Bad Request<\/strong> response with an error message.<\/li>\n\n\n\n<li><strong><code>NotFound()<\/code><\/strong>: Returns a <strong>404 Not Found<\/strong> response.<\/li>\n<\/ul>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>Real-Life Example<\/strong>: In a financial application, you might use <code>IActionResult<\/code> to return different responses based on whether the transaction was successful (<code>Ok<\/code>) or if there was an error (<code>BadRequest<\/code>).<\/p>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>2. MapControllerRoute and How to Use It<\/strong><\/h2>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>What is <code>MapControllerRoute<\/code>?<\/strong> <code>MapControllerRoute<\/code> is used to define <strong>conventional routing<\/strong> in ASP.NET Core. It allows you to create routes that can map to your controllers and actions using a URL pattern defined in the <strong><code>Program.cs<\/code><\/strong> file.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>Example of Using <code>MapControllerRoute<\/code><\/strong>:<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: csharp; title: ; notranslate\" title=\"\">\nvar builder = WebApplication.CreateBuilder(args);\nvar app = builder.Build();\n\napp.UseRouting();\n\napp.UseEndpoints(endpoints =&gt;\n{\n    endpoints.MapControllerRoute(\n        name: &quot;default&quot;,\n        pattern: &quot;{controller=Home}\/{action=Index}\/{id?}&quot;);\n});\n\napp.Run();\n<\/pre><\/div>\n\n\n<ul class=\"wp-block-list\">\n<li><strong>Pattern<\/strong>: <code>\"default\"<\/code> is the name of the route, and <code>\"pattern\"<\/code> is the format of the URL.\n<ul class=\"wp-block-list\">\n<li><code>{controller=Home}<\/code>: Indicates the default controller is &quot;Home&quot; if none is specified.<\/li>\n\n\n\n<li><code>{action=Index}<\/code>: Indicates the default action is &quot;Index&quot; if none is specified.<\/li>\n\n\n\n<li><code>{id?}<\/code>: The <code>id<\/code> parameter is optional, denoted by <code>?<\/code>.<\/li>\n<\/ul>\n<\/li>\n<\/ul>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>When to Use <code>MapControllerRoute<\/code><\/strong>:<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>When you need to define a central set of routes that are <strong>shared across controllers<\/strong>.<\/li>\n\n\n\n<li>When your application has a <strong>standard URL structure<\/strong>.<\/li>\n<\/ul>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>3. Attribute Routing vs. Conventional Routing<\/strong><\/h2>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>Attribute Routing<\/strong>:<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Routing rules are defined using <strong>attributes<\/strong> directly on controller classes and methods.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Example:<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: csharp; title: ; notranslate\" title=\"\">\n&#x5B;ApiController]\n&#x5B;Route(&quot;api\/products&quot;)]\npublic class ProductController : ControllerBase\n{\n    &#x5B;HttpGet(&quot;{id}&quot;)]\n    public IActionResult GetProduct(int id)\n    {\n        return Ok(new Product { Id = id, Name = &quot;Sample Product&quot;, Price = 10.0m });\n    }\n}\n<\/pre><\/div>\n\n\n<p class=\"wp-block-paragraph\"><code>[Route(\"api\/products\")]<\/code> defines the base route for the controller.<code>[HttpGet(\"{id}\")]<\/code> defines the route for an individual action, where <code>{id}<\/code> is a parameter.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>Conventional Routing<\/strong>:<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Routing is defined <strong>centrally<\/strong> in the <code>Program.cs<\/code> file using <code>MapControllerRoute<\/code>.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">Example:<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: csharp; title: ; notranslate\" title=\"\">\napp.UseEndpoints(endpoints =&gt;\n{\n    endpoints.MapControllerRoute(\n        name: &quot;default&quot;,\n        pattern: &quot;{controller=Home}\/{action=Index}\/{id?}&quot;);\n});\n<\/pre><\/div>\n\n\n<p class=\"wp-block-paragraph\">Conventional routing is easier to <strong>manage for small projects<\/strong> where there is a predictable pattern.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>Difference<\/strong>:<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li><strong>Attribute Routing<\/strong> gives you <strong>fine-grained control<\/strong> over how each endpoint is exposed and is preferred for building REST APIs since it makes routes more explicit.<\/li>\n\n\n\n<li><strong>Conventional Routing<\/strong> is better for larger MVC projects where URLs follow a predictable pattern, and you want to configure them centrally.<\/li>\n<\/ul>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>Real-Life Example<\/strong>: In an e-commerce application:<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li><strong>Attribute Routing<\/strong> might be used for APIs (<code>\/api\/products\/{id}<\/code>) where each endpoint must be clearly defined.<\/li>\n\n\n\n<li><strong>Conventional Routing<\/strong> might be used for the customer-facing part of the website (<code>\/products\/list<\/code> or <code>\/products\/details\/{id}<\/code>) where there is a common structure.<\/li>\n<\/ul>\n\n\n\n<h2 class=\"wp-block-heading\"><strong>4. Endpoints: What Are They and How to Use Them<\/strong><\/h2>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>What are Endpoints?<\/strong> Endpoints are <strong>units of routing information<\/strong> that tell ASP.NET Core where to send a specific request. Each endpoint represents a <strong>controller action<\/strong> or a <strong>route handler<\/strong> that handles a specific request.<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>An endpoint could be a <strong>controller action<\/strong> (<code>\/api\/products<\/code>), a <strong>Razor page<\/strong>, or a <strong>custom delegate<\/strong>.<\/li>\n\n\n\n<li>Endpoints are registered using the <code>MapControllers()<\/code>, <code>MapGet()<\/code>, <code>MapPost()<\/code>, etc., methods.<\/li>\n<\/ul>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>Example<\/strong>:<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: csharp; title: ; notranslate\" title=\"\">\napp.UseEndpoints(endpoints =&gt;\n{\n    endpoints.MapControllers(); \/\/ Automatically maps all controllers\n    endpoints.MapGet(&quot;\/status&quot;, async context =&gt;\n    {\n        await context.Response.WriteAsync(&quot;API is running!&quot;);\n    });\n});\n<\/pre><\/div>\n\n\n<ul class=\"wp-block-list\">\n<li><strong><code>MapControllers()<\/code><\/strong>: Maps all controllers with attribute routing to endpoints.<\/li>\n\n\n\n<li><strong><code>MapGet()<\/code><\/strong>: Creates a simple endpoint to respond to a GET request.<\/li>\n<\/ul>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>When to Use Endpoints<\/strong>:<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>Use <strong><code>MapControllers()<\/code><\/strong> for API controllers.<\/li>\n\n\n\n<li>Use <strong><code>MapGet()<\/code>, <code>MapPost()<\/code><\/strong>, etc., for simple endpoints when you need <strong>custom, lightweight handlers<\/strong> (e.g., a health check route).<\/li>\n<\/ul>\n\n\n\n<p class=\"wp-block-paragraph\"><strong>Real-Life Example<\/strong>:<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>In an e-commerce API, <strong>endpoints<\/strong> for listing products (<code>GET \/api\/products<\/code>) or creating new orders (<code>POST \/api\/orders<\/code>) are defined to handle specific functionality.<\/li>\n\n\n\n<li>You may also define an endpoint like <code>\/status<\/code> to verify that your API is running properly.<\/li>\n<\/ul>\n\n\n\n<h3 class=\"wp-block-heading\">Summary<\/h3>\n\n\n\n<ul class=\"wp-block-list\">\n<li><strong><code>IActionResult<\/code><\/strong>: Represents different types of HTTP responses and is used to provide flexibility in returning responses from controller actions.<\/li>\n\n\n\n<li><strong><code>MapControllerRoute<\/code><\/strong>: Used for defining <strong>conventional routing<\/strong> in a central way in the <code>Program.cs<\/code> file, making it useful for creating common patterns across multiple controllers.<\/li>\n\n\n\n<li><strong>Attribute Routing vs. Conventional Routing<\/strong>:\n<ul class=\"wp-block-list\">\n<li><strong>Attribute Routing<\/strong> is more <strong>explicit<\/strong> and gives better control for API development.<\/li>\n\n\n\n<li><strong>Conventional Routing<\/strong> is <strong>centralized<\/strong> and works well for apps with a common structure.<\/li>\n<\/ul>\n<\/li>\n\n\n\n<li><strong>Endpoints<\/strong>: Represent routing information and are used to determine which controller action should handle a request. They are registered in <code>UseEndpoints()<\/code> for better control over the routing pipeline.<\/li>\n<\/ul>\n\n\n\n<h2 class=\"wp-block-heading\">Project Structure Overview<\/h2>\n\n\n\n<ul class=\"wp-block-list\">\n<li><strong>Controllers<\/strong>: <code>ProductController<\/code> to handle CRUD operations for products.<\/li>\n\n\n\n<li><strong>Routing<\/strong>: Attribute routing in the controller, and conventional routing set up in <code>Program.cs<\/code>.<\/li>\n\n\n\n<li><strong>Middleware<\/strong>: Middleware is added for logging requests.<\/li>\n\n\n\n<li><strong>Dependency Injection<\/strong>: <code>IProductService<\/code> is used to manage product operations.<\/li>\n<\/ul>\n\n\n\n<h3 class=\"wp-block-heading\"><strong>Code for a Full ASP.NET Core Web API Program<\/strong><\/h3>\n\n\n\n<h4 class=\"wp-block-heading\"><strong>Step 1: Create a Product Model<\/strong><\/h4>\n\n\n\n<p class=\"wp-block-paragraph\">Create a <code>Product<\/code> model to represent products in the API.<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: csharp; title: ; notranslate\" title=\"\">\npublic class Product\n{\n    public int Id { get; set; }\n    public string Name { get; set; }\n    public decimal Price { get; set; }\n}\n<\/pre><\/div>\n\n\n<h4 class=\"wp-block-heading\"><strong>Step 2: Create a Product Service Interface and Implementation<\/strong><\/h4>\n\n\n\n<p class=\"wp-block-paragraph\">Define an interface <code>IProductService<\/code> and its implementation <code>ProductService<\/code>.<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: csharp; title: ; notranslate\" title=\"\">\npublic interface IProductService\n{\n    List&lt;Product&gt; GetAllProducts();\n    Product GetProductById(int id);\n    void AddProduct(Product product);\n}\n\npublic class ProductService : IProductService\n{\n    private readonly List&lt;Product&gt; _products = new List&lt;Product&gt;\n    {\n        new Product { Id = 1, Name = &quot;Laptop&quot;, Price = 1000.00m },\n        new Product { Id = 2, Name = &quot;Phone&quot;, Price = 500.00m }\n    };\n\n    public List&lt;Product&gt; GetAllProducts() =&gt; _products;\n\n    public Product GetProductById(int id) =&gt; _products.FirstOrDefault(p =&gt; p.Id == id);\n\n    public void AddProduct(Product product) =&gt; _products.Add(product);\n}\n<\/pre><\/div>\n\n\n<h4 class=\"wp-block-heading\"><strong>Step 3: Create the Product Controller<\/strong><\/h4>\n\n\n\n<p class=\"wp-block-paragraph\">The <code>ProductController<\/code> will handle HTTP requests for managing products.<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: csharp; title: ; notranslate\" title=\"\">\nusing Microsoft.AspNetCore.Mvc;\n\n&#x5B;ApiController]\n&#x5B;Route(&quot;api\/&#x5B;controller]&quot;)]\npublic class ProductController : ControllerBase\n{\n    private readonly IProductService _productService;\n\n    public ProductController(IProductService productService)\n    {\n        _productService = productService;\n    }\n\n    &#x5B;HttpGet]\n    public IActionResult GetProducts()\n    {\n        var products = _productService.GetAllProducts();\n        return Ok(products);\n    }\n\n    &#x5B;HttpGet(&quot;{id}&quot;)]\n    public IActionResult GetProductById(int id)\n    {\n        var product = _productService.GetProductById(id);\n        if (product == null)\n        {\n            return NotFound();\n        }\n        return Ok(product);\n    }\n\n    &#x5B;HttpPost]\n    public IActionResult AddProduct(&#x5B;FromBody] Product product)\n    {\n        _productService.AddProduct(product);\n        return CreatedAtAction(nameof(GetProductById), new { id = product.Id }, product);\n    }\n}\n<\/pre><\/div>\n\n\n<ul class=\"wp-block-list\">\n<li><strong>Attribute Routing<\/strong>: Each action uses attribute routing, such as <code>[HttpGet(\"{id}\")]<\/code> to match <code>\/api\/product\/{id}<\/code>.<\/li>\n\n\n\n<li><strong><code>IActionResult<\/code><\/strong>: Used to return different types of HTTP responses (e.g., <code>Ok()<\/code>, <code>NotFound()<\/code>, <code>CreatedAtAction()<\/code>).<\/li>\n<\/ul>\n\n\n\n<h4 class=\"wp-block-heading\"><strong>Step 4: Configure Middleware and Routing in <code>Program.cs<\/code><\/strong><\/h4>\n\n\n\n<p class=\"wp-block-paragraph\">Configure dependency injection, middleware, and routing in the entry point of the application.<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: csharp; title: ; notranslate\" title=\"\">\nvar builder = WebApplication.CreateBuilder(args);\n\n\/\/ Add services to the container.\nbuilder.Services.AddControllers();\nbuilder.Services.AddScoped&lt;IProductService, ProductService&gt;();\n\nvar app = builder.Build();\n\n\/\/ Add middleware for logging requests\napp.Use(async (context, next) =&gt;\n{\n    Console.WriteLine($&quot;Incoming request: {context.Request.Method} {context.Request.Path}&quot;);\n    await next.Invoke();\n});\n\n\/\/ Use routing and endpoints\napp.UseRouting();\napp.UseAuthorization();\n\napp.UseEndpoints(endpoints =&gt;\n{\n    \/\/ Conventional Routing\n    endpoints.MapControllerRoute(\n        name: &quot;default&quot;,\n        pattern: &quot;{controller=Home}\/{action=Index}\/{id?}&quot;);\n\n    \/\/ Attribute Routing\n    endpoints.MapControllers();\n});\n\n\/\/ Run the application\napp.Run();\n<\/pre><\/div>\n\n\n<h4 class=\"wp-block-heading\"><strong>Step 5: Example Requests<\/strong><\/h4>\n\n\n\n<p class=\"wp-block-paragraph\">Here's how different components of the program come together when interacting with the API.<\/p>\n\n\n\n<ol class=\"wp-block-list\">\n<li><strong>Request<\/strong>: <code>GET \/api\/product<\/code>\n<ul class=\"wp-block-list\">\n<li><strong>Handled by<\/strong>: <code>ProductController.GetProducts()<\/code><\/li>\n\n\n\n<li><strong>Response<\/strong>: Returns a list of all products (<code>200 OK<\/code>).<\/li>\n<\/ul>\n<\/li>\n\n\n\n<li><strong>Request<\/strong>: <code>GET \/api\/product\/1<\/code>\n<ul class=\"wp-block-list\">\n<li><strong>Handled by<\/strong>: <code>ProductController.GetProductById(int id)<\/code><\/li>\n\n\n\n<li><strong>Response<\/strong>: Returns the product with ID 1, or <code>404 Not Found<\/code> if it doesn't exist.<\/li>\n<\/ul>\n<\/li>\n\n\n\n<li><strong>Request<\/strong>: <code>POST \/api\/product<\/code>\n<ul class=\"wp-block-list\">\n<li><strong>Handled by<\/strong>: <code>ProductController.AddProduct(Product product)<\/code><\/li>\n\n\n\n<li><strong>Body<\/strong>: A JSON object representing the new product.<\/li>\n\n\n\n<li><strong>Response<\/strong>: Returns <code>201 Created<\/code> with the location of the newly created product.<\/li>\n<\/ul>\n<\/li>\n<\/ol>\n\n\n\n<h3 class=\"wp-block-heading\"><strong>Explanation of Key Concepts<\/strong><\/h3>\n\n\n\n<ol class=\"wp-block-list\">\n<li><strong>Attribute Routing vs. Conventional Routing<\/strong>:\n<ul class=\"wp-block-list\">\n<li><strong>Attribute Routing<\/strong>: Used in <code>ProductController<\/code> to specify routes directly on actions.<\/li>\n\n\n\n<li><strong>Conventional Routing<\/strong>: Defined in <code>Program.cs<\/code> using <code>MapControllerRoute<\/code>, which provides a default route for controllers.<\/li>\n<\/ul>\n<\/li>\n\n\n\n<li><strong>Middleware<\/strong>:\n<ul class=\"wp-block-list\">\n<li>Custom middleware in <code>Program.cs<\/code> logs incoming requests.<\/li>\n\n\n\n<li>Middleware is executed in the order it\u2019s added to the pipeline.<\/li>\n<\/ul>\n<\/li>\n\n\n\n<li><strong>Dependency Injection<\/strong>:\n<ul class=\"wp-block-list\">\n<li><strong><code>IProductService<\/code><\/strong> is registered in the DI container (<code>builder.Services.AddScoped&lt;IProductService, ProductService>()<\/code>).<\/li>\n\n\n\n<li>The <code>ProductController<\/code> receives <code>IProductService<\/code> via constructor injection, which is a best practice to decouple the service logic from the controller.<\/li>\n<\/ul>\n<\/li>\n\n\n\n<li><strong>Endpoints<\/strong>:\n<ul class=\"wp-block-list\">\n<li><code>MapControllers()<\/code> maps all controllers that use attribute routing.<\/li>\n\n\n\n<li><strong>Conventional routing<\/strong> via <code>MapControllerRoute()<\/code> provides a fallback default route.<\/li>\n<\/ul>\n<\/li>\n<\/ol>\n\n\n\n<h3 class=\"wp-block-heading\"><strong>Summary<\/strong><\/h3>\n\n\n\n<p class=\"wp-block-paragraph\">This full example demonstrates how different ASP.NET Core Web API components work together:<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li><strong>Controllers<\/strong> handle incoming requests.<\/li>\n\n\n\n<li><strong>Routing<\/strong> (attribute and conventional) helps map these requests to the right actions.<\/li>\n\n\n\n<li><strong>Middleware<\/strong> processes requests and responses, e.g., logging them.<\/li>\n\n\n\n<li><strong>Dependency Injection<\/strong> helps keep code modular and testable.<\/li>\n<\/ul>\n","protected":false},"excerpt":{"rendered":"<p>What is Routing? Routing in ASP.NET Core is the process of mapping an incoming HTTP request to a particular controller action. When a request is made to an API, routing helps to determine which action should handle it based on the URL and other request parameters. There are two main types of routing: 1. IActionResult [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"ocean_post_layout":"","ocean_both_sidebars_style":"","ocean_both_sidebars_content_width":0,"ocean_both_sidebars_sidebars_width":0,"ocean_sidebar":"","ocean_second_sidebar":"","ocean_disable_margins":"enable","ocean_add_body_class":"","ocean_shortcode_before_top_bar":"","ocean_shortcode_after_top_bar":"","ocean_shortcode_before_header":"","ocean_shortcode_after_header":"","ocean_has_shortcode":"","ocean_shortcode_after_title":"","ocean_shortcode_before_footer_widgets":"","ocean_shortcode_after_footer_widgets":"","ocean_shortcode_before_footer_bottom":"","ocean_shortcode_after_footer_bottom":"","ocean_display_top_bar":"default","ocean_display_header":"default","ocean_header_style":"","ocean_center_header_left_menu":"","ocean_custom_header_template":"","ocean_custom_logo":0,"ocean_custom_retina_logo":0,"ocean_custom_logo_max_width":0,"ocean_custom_logo_tablet_max_width":0,"ocean_custom_logo_mobile_max_width":0,"ocean_custom_logo_max_height":0,"ocean_custom_logo_tablet_max_height":0,"ocean_custom_logo_mobile_max_height":0,"ocean_header_custom_menu":"","ocean_menu_typo_font_family":"","ocean_menu_typo_font_subset":"","ocean_menu_typo_font_size":0,"ocean_menu_typo_font_size_tablet":0,"ocean_menu_typo_font_size_mobile":0,"ocean_menu_typo_font_size_unit":"px","ocean_menu_typo_font_weight":"","ocean_menu_typo_font_weight_tablet":"","ocean_menu_typo_font_weight_mobile":"","ocean_menu_typo_transform":"","ocean_menu_typo_transform_tablet":"","ocean_menu_typo_transform_mobile":"","ocean_menu_typo_line_height":0,"ocean_menu_typo_line_height_tablet":0,"ocean_menu_typo_line_height_mobile":0,"ocean_menu_typo_line_height_unit":"","ocean_menu_typo_spacing":0,"ocean_menu_typo_spacing_tablet":0,"ocean_menu_typo_spacing_mobile":0,"ocean_menu_typo_spacing_unit":"","ocean_menu_link_color":"","ocean_menu_link_color_hover":"","ocean_menu_link_color_active":"","ocean_menu_link_background":"","ocean_menu_link_hover_background":"","ocean_menu_link_active_background":"","ocean_menu_social_links_bg":"","ocean_menu_social_hover_links_bg":"","ocean_menu_social_links_color":"","ocean_menu_social_hover_links_color":"","ocean_disable_title":"default","ocean_disable_heading":"default","ocean_post_title":"","ocean_post_subheading":"","ocean_post_title_style":"","ocean_post_title_background_color":"","ocean_post_title_background":0,"ocean_post_title_bg_image_position":"","ocean_post_title_bg_image_attachment":"","ocean_post_title_bg_image_repeat":"","ocean_post_title_bg_image_size":"","ocean_post_title_height":0,"ocean_post_title_bg_overlay":0.5,"ocean_post_title_bg_overlay_color":"","ocean_disable_breadcrumbs":"default","ocean_breadcrumbs_color":"","ocean_breadcrumbs_separator_color":"","ocean_breadcrumbs_links_color":"","ocean_breadcrumbs_links_hover_color":"","ocean_display_footer_widgets":"default","ocean_display_footer_bottom":"default","ocean_custom_footer_template":"","_jetpack_newsletter_access":"","_jetpack_dont_email_post_to_subs":false,"_jetpack_newsletter_tier_id":0,"_jetpack_memberships_contains_paywalled_content":false,"_jetpack_memberships_contains_paid_content":false,"ocean_post_oembed":"","ocean_post_self_hosted_media":"","ocean_post_video_embed":"","ocean_link_format":"","ocean_link_format_target":"self","ocean_quote_format":"","ocean_quote_format_link":"post","ocean_gallery_link_images":"on","ocean_gallery_id":[],"footnotes":""},"categories":[79],"tags":[],"class_list":["post-1969","post","type-post","status-publish","format-standard","hentry","category-dotnet-8","entry"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v27.9 - https:\/\/yoast.com\/product\/yoast-seo-wordpress\/ -->\n<title>Routing in ASP.NET Core - Code Notebook<\/title>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/epicmarketing.co.il\/notebook\/routing-in-asp-net-core\/\" \/>\n<meta property=\"og:locale\" content=\"he_IL\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"Routing in ASP.NET Core - Code Notebook\" \/>\n<meta property=\"og:description\" content=\"What is Routing? Routing in ASP.NET Core is the process of mapping an incoming HTTP request to a particular controller action. When a request is made to an API, routing helps to determine which action should handle it based on the URL and other request parameters. There are two main types of routing: 1. IActionResult [&hellip;]\" \/>\n<meta property=\"og:url\" content=\"https:\/\/epicmarketing.co.il\/notebook\/routing-in-asp-net-core\/\" \/>\n<meta property=\"og:site_name\" content=\"Code Notebook\" \/>\n<meta property=\"article:published_time\" content=\"2024-10-31T15:10:02+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2024-10-31T15:14:38+00:00\" \/>\n<meta name=\"author\" content=\"kerendanino\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:label1\" content=\"\u05e0\u05db\u05ea\u05d1 \u05e2\u05dc \u05d9\u05d3\" \/>\n\t<meta name=\"twitter:data1\" content=\"kerendanino\" \/>\n\t<meta name=\"twitter:label2\" content=\"\u05d6\u05de\u05df \u05e7\u05e8\u05d9\u05d0\u05d4 \u05de\u05d5\u05e2\u05e8\u05da\" \/>\n\t<meta name=\"twitter:data2\" content=\"6 \u05d3\u05e7\u05d5\u05ea\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\\\/\\\/schema.org\",\"@graph\":[{\"@type\":\"Article\",\"@id\":\"https:\\\/\\\/epicmarketing.co.il\\\/notebook\\\/routing-in-asp-net-core\\\/#article\",\"isPartOf\":{\"@id\":\"https:\\\/\\\/epicmarketing.co.il\\\/notebook\\\/routing-in-asp-net-core\\\/\"},\"author\":{\"name\":\"kerendanino\",\"@id\":\"https:\\\/\\\/epicmarketing.co.il\\\/notebook\\\/#\\\/schema\\\/person\\\/195dfc625818eadda7903d456890e24c\"},\"headline\":\"Routing in ASP.NET Core\",\"datePublished\":\"2024-10-31T15:10:02+00:00\",\"dateModified\":\"2024-10-31T15:14:38+00:00\",\"mainEntityOfPage\":{\"@id\":\"https:\\\/\\\/epicmarketing.co.il\\\/notebook\\\/routing-in-asp-net-core\\\/\"},\"wordCount\":1051,\"commentCount\":0,\"publisher\":{\"@id\":\"https:\\\/\\\/epicmarketing.co.il\\\/notebook\\\/#organization\"},\"articleSection\":[\"Dotnet 8\"],\"inLanguage\":\"he-IL\",\"potentialAction\":[{\"@type\":\"CommentAction\",\"name\":\"Comment\",\"target\":[\"https:\\\/\\\/epicmarketing.co.il\\\/notebook\\\/routing-in-asp-net-core\\\/#respond\"]}]},{\"@type\":\"WebPage\",\"@id\":\"https:\\\/\\\/epicmarketing.co.il\\\/notebook\\\/routing-in-asp-net-core\\\/\",\"url\":\"https:\\\/\\\/epicmarketing.co.il\\\/notebook\\\/routing-in-asp-net-core\\\/\",\"name\":\"Routing in ASP.NET Core - Code Notebook\",\"isPartOf\":{\"@id\":\"https:\\\/\\\/epicmarketing.co.il\\\/notebook\\\/#website\"},\"datePublished\":\"2024-10-31T15:10:02+00:00\",\"dateModified\":\"2024-10-31T15:14:38+00:00\",\"breadcrumb\":{\"@id\":\"https:\\\/\\\/epicmarketing.co.il\\\/notebook\\\/routing-in-asp-net-core\\\/#breadcrumb\"},\"inLanguage\":\"he-IL\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\\\/\\\/epicmarketing.co.il\\\/notebook\\\/routing-in-asp-net-core\\\/\"]}]},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\\\/\\\/epicmarketing.co.il\\\/notebook\\\/routing-in-asp-net-core\\\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\\\/\\\/epicmarketing.co.il\\\/notebook\\\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"Routing in ASP.NET Core\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\\\/\\\/epicmarketing.co.il\\\/notebook\\\/#website\",\"url\":\"https:\\\/\\\/epicmarketing.co.il\\\/notebook\\\/\",\"name\":\"Code Notebook\",\"description\":\"Easy coding\",\"publisher\":{\"@id\":\"https:\\\/\\\/epicmarketing.co.il\\\/notebook\\\/#organization\"},\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\\\/\\\/epicmarketing.co.il\\\/notebook\\\/?s={search_term_string}\"},\"query-input\":{\"@type\":\"PropertyValueSpecification\",\"valueRequired\":true,\"valueName\":\"search_term_string\"}}],\"inLanguage\":\"he-IL\"},{\"@type\":\"Organization\",\"@id\":\"https:\\\/\\\/epicmarketing.co.il\\\/notebook\\\/#organization\",\"name\":\"Code Notebook\",\"url\":\"https:\\\/\\\/epicmarketing.co.il\\\/notebook\\\/\",\"logo\":{\"@type\":\"ImageObject\",\"inLanguage\":\"he-IL\",\"@id\":\"https:\\\/\\\/epicmarketing.co.il\\\/notebook\\\/#\\\/schema\\\/logo\\\/image\\\/\",\"url\":\"https:\\\/\\\/epicmarketing.co.il\\\/notebook\\\/wp-content\\\/uploads\\\/2023\\\/07\\\/logo-epic-marketing-05.png\",\"contentUrl\":\"https:\\\/\\\/epicmarketing.co.il\\\/notebook\\\/wp-content\\\/uploads\\\/2023\\\/07\\\/logo-epic-marketing-05.png\",\"width\":3626,\"height\":1942,\"caption\":\"Code Notebook\"},\"image\":{\"@id\":\"https:\\\/\\\/epicmarketing.co.il\\\/notebook\\\/#\\\/schema\\\/logo\\\/image\\\/\"}},{\"@type\":\"Person\",\"@id\":\"https:\\\/\\\/epicmarketing.co.il\\\/notebook\\\/#\\\/schema\\\/person\\\/195dfc625818eadda7903d456890e24c\",\"name\":\"kerendanino\",\"image\":{\"@type\":\"ImageObject\",\"inLanguage\":\"he-IL\",\"@id\":\"https:\\\/\\\/secure.gravatar.com\\\/avatar\\\/285cc9389c66aa46da1e26a474b1e90e9efaf3fa21f1b928cbd63ce5f0e89c63?s=96&d=mm&r=g\",\"url\":\"https:\\\/\\\/secure.gravatar.com\\\/avatar\\\/285cc9389c66aa46da1e26a474b1e90e9efaf3fa21f1b928cbd63ce5f0e89c63?s=96&d=mm&r=g\",\"contentUrl\":\"https:\\\/\\\/secure.gravatar.com\\\/avatar\\\/285cc9389c66aa46da1e26a474b1e90e9efaf3fa21f1b928cbd63ce5f0e89c63?s=96&d=mm&r=g\",\"caption\":\"kerendanino\"},\"url\":\"https:\\\/\\\/epicmarketing.co.il\\\/notebook\\\/author\\\/kerendanino\\\/\"}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"Routing in ASP.NET Core - Code Notebook","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/epicmarketing.co.il\/notebook\/routing-in-asp-net-core\/","og_locale":"he_IL","og_type":"article","og_title":"Routing in ASP.NET Core - Code Notebook","og_description":"What is Routing? Routing in ASP.NET Core is the process of mapping an incoming HTTP request to a particular controller action. When a request is made to an API, routing helps to determine which action should handle it based on the URL and other request parameters. There are two main types of routing: 1. IActionResult [&hellip;]","og_url":"https:\/\/epicmarketing.co.il\/notebook\/routing-in-asp-net-core\/","og_site_name":"Code Notebook","article_published_time":"2024-10-31T15:10:02+00:00","article_modified_time":"2024-10-31T15:14:38+00:00","author":"kerendanino","twitter_card":"summary_large_image","twitter_misc":{"\u05e0\u05db\u05ea\u05d1 \u05e2\u05dc \u05d9\u05d3":"kerendanino","\u05d6\u05de\u05df \u05e7\u05e8\u05d9\u05d0\u05d4 \u05de\u05d5\u05e2\u05e8\u05da":"6 \u05d3\u05e7\u05d5\u05ea"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"https:\/\/epicmarketing.co.il\/notebook\/routing-in-asp-net-core\/#article","isPartOf":{"@id":"https:\/\/epicmarketing.co.il\/notebook\/routing-in-asp-net-core\/"},"author":{"name":"kerendanino","@id":"https:\/\/epicmarketing.co.il\/notebook\/#\/schema\/person\/195dfc625818eadda7903d456890e24c"},"headline":"Routing in ASP.NET Core","datePublished":"2024-10-31T15:10:02+00:00","dateModified":"2024-10-31T15:14:38+00:00","mainEntityOfPage":{"@id":"https:\/\/epicmarketing.co.il\/notebook\/routing-in-asp-net-core\/"},"wordCount":1051,"commentCount":0,"publisher":{"@id":"https:\/\/epicmarketing.co.il\/notebook\/#organization"},"articleSection":["Dotnet 8"],"inLanguage":"he-IL","potentialAction":[{"@type":"CommentAction","name":"Comment","target":["https:\/\/epicmarketing.co.il\/notebook\/routing-in-asp-net-core\/#respond"]}]},{"@type":"WebPage","@id":"https:\/\/epicmarketing.co.il\/notebook\/routing-in-asp-net-core\/","url":"https:\/\/epicmarketing.co.il\/notebook\/routing-in-asp-net-core\/","name":"Routing in ASP.NET Core - Code Notebook","isPartOf":{"@id":"https:\/\/epicmarketing.co.il\/notebook\/#website"},"datePublished":"2024-10-31T15:10:02+00:00","dateModified":"2024-10-31T15:14:38+00:00","breadcrumb":{"@id":"https:\/\/epicmarketing.co.il\/notebook\/routing-in-asp-net-core\/#breadcrumb"},"inLanguage":"he-IL","potentialAction":[{"@type":"ReadAction","target":["https:\/\/epicmarketing.co.il\/notebook\/routing-in-asp-net-core\/"]}]},{"@type":"BreadcrumbList","@id":"https:\/\/epicmarketing.co.il\/notebook\/routing-in-asp-net-core\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/epicmarketing.co.il\/notebook\/"},{"@type":"ListItem","position":2,"name":"Routing in ASP.NET Core"}]},{"@type":"WebSite","@id":"https:\/\/epicmarketing.co.il\/notebook\/#website","url":"https:\/\/epicmarketing.co.il\/notebook\/","name":"Code Notebook","description":"Easy coding","publisher":{"@id":"https:\/\/epicmarketing.co.il\/notebook\/#organization"},"potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/epicmarketing.co.il\/notebook\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"he-IL"},{"@type":"Organization","@id":"https:\/\/epicmarketing.co.il\/notebook\/#organization","name":"Code Notebook","url":"https:\/\/epicmarketing.co.il\/notebook\/","logo":{"@type":"ImageObject","inLanguage":"he-IL","@id":"https:\/\/epicmarketing.co.il\/notebook\/#\/schema\/logo\/image\/","url":"https:\/\/epicmarketing.co.il\/notebook\/wp-content\/uploads\/2023\/07\/logo-epic-marketing-05.png","contentUrl":"https:\/\/epicmarketing.co.il\/notebook\/wp-content\/uploads\/2023\/07\/logo-epic-marketing-05.png","width":3626,"height":1942,"caption":"Code Notebook"},"image":{"@id":"https:\/\/epicmarketing.co.il\/notebook\/#\/schema\/logo\/image\/"}},{"@type":"Person","@id":"https:\/\/epicmarketing.co.il\/notebook\/#\/schema\/person\/195dfc625818eadda7903d456890e24c","name":"kerendanino","image":{"@type":"ImageObject","inLanguage":"he-IL","@id":"https:\/\/secure.gravatar.com\/avatar\/285cc9389c66aa46da1e26a474b1e90e9efaf3fa21f1b928cbd63ce5f0e89c63?s=96&d=mm&r=g","url":"https:\/\/secure.gravatar.com\/avatar\/285cc9389c66aa46da1e26a474b1e90e9efaf3fa21f1b928cbd63ce5f0e89c63?s=96&d=mm&r=g","contentUrl":"https:\/\/secure.gravatar.com\/avatar\/285cc9389c66aa46da1e26a474b1e90e9efaf3fa21f1b928cbd63ce5f0e89c63?s=96&d=mm&r=g","caption":"kerendanino"},"url":"https:\/\/epicmarketing.co.il\/notebook\/author\/kerendanino\/"}]}},"jetpack_featured_media_url":"","jetpack_sharing_enabled":true,"_links":{"self":[{"href":"https:\/\/epicmarketing.co.il\/notebook\/wp-json\/wp\/v2\/posts\/1969","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/epicmarketing.co.il\/notebook\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/epicmarketing.co.il\/notebook\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/epicmarketing.co.il\/notebook\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/epicmarketing.co.il\/notebook\/wp-json\/wp\/v2\/comments?post=1969"}],"version-history":[{"count":2,"href":"https:\/\/epicmarketing.co.il\/notebook\/wp-json\/wp\/v2\/posts\/1969\/revisions"}],"predecessor-version":[{"id":1973,"href":"https:\/\/epicmarketing.co.il\/notebook\/wp-json\/wp\/v2\/posts\/1969\/revisions\/1973"}],"wp:attachment":[{"href":"https:\/\/epicmarketing.co.il\/notebook\/wp-json\/wp\/v2\/media?parent=1969"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/epicmarketing.co.il\/notebook\/wp-json\/wp\/v2\/categories?post=1969"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/epicmarketing.co.il\/notebook\/wp-json\/wp\/v2\/tags?post=1969"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}