ASP .NET Attribute Routing Tutorial šŸŽÆ

beginner
6 min

ASP .NET Attribute Routing Tutorial šŸŽÆ

Welcome back to CodeYourCraft! Today, we're diving into Attribute Routing – a powerful and flexible way to define routes in ASP .NET. This technique will help you create cleaner, more maintainable URLs for your web applications. Let's get started!

Understanding Attribute Routing šŸ“

Attribute Routing is a feature that allows you to define routes using attributes directly on your controller actions. This is a significant departure from the traditional method, where routes are defined in the RouteConfig.cs file.

Why Attribute Routing? šŸ’”

  • Simplicity: Attribute Routing makes your code more readable and easier to understand.
  • Flexibility: You can define routes at the action level, allowing for more complex routing scenarios.
  • Reusability: You can create custom attributes to handle specific routing requirements.

Getting Started with Attribute Routing šŸ’”

Let's create a simple example to illustrate Attribute Routing.

Prerequisites

  • .NET Framework 4.5 or later
  • Visual Studio 2013 or later

Creating a New Project šŸ“

  1. Open Visual Studio and create a new ASP .NET Web Application project.
  2. Choose the "Empty" template and name your project AttributeRoutingExample.

Adding Attribute Routing šŸ’”

  1. Right-click on the Controllers folder and select Add > Controller. Name the new controller ValuesController.

  2. In the ValuesController.cs file, replace the default code with the following:

csharp
using System; using System.Web.Mvc; using System.Web.Routing; namespace AttributeRoutingExample.Controllers { public class ValuesController : Controller { // GET: Values [Route("api/[controller]")] public ActionResult Index() { return View(); } // GET: Values/5 [Route("api/values/{id}")] public ActionResult Details(int id) { return Content("Value: " + id); } } }

šŸ’” Pro Tip: The Route attribute is what enables Attribute Routing.

Testing Attribute Routing šŸ’”

  1. Run the application and navigate to http://localhost:<port>/api/values in your browser. You should see the view from the Index action.

  2. Navigate to http://localhost:<port>/api/values/5, replacing <port> with the port number displayed in Visual Studio. You should see the content "Value: 5".

Custom Attribute Routing šŸ’”

You can create custom attributes to handle specific routing requirements. Let's create a custom attribute for handling JSON responses.

Creating a Custom Attribute šŸ“

  1. Right-click on the project and select Add > New Item.

  2. Choose Class and name it JsonOutputAttribute.

  3. Replace the default code with the following:

csharp
using System; using System.Web.Mvc; using System.Web.Routing; namespace AttributeRoutingExample { public class JsonOutputAttribute : ActionFilterAttribute { public override void OnResultExecuted(ResultExecutedContext filterContext) { if (filterContext.Result is JsonResult jsonResult) { filterContext.HttpContext.Response.ContentType = "application/json"; } base.OnResultExecuted(filterContext); } } }

šŸ’” Pro Tip: The ActionFilterAttribute base class allows us to execute custom logic before or after the action is executed.

Using the Custom Attribute šŸ’”

  1. Modify the ValuesController.cs file to use the custom attribute:
csharp
using System; using System.Web.Mvc; using System.Web.Routing; namespace AttributeRoutingExample.Controllers { public class ValuesController : Controller { [Route("api/[controller]")] [JsonOutput] public ActionResult Index() { return Json(new { message = "Welcome to Attribute Routing!" }); } // GET: Values/5 [Route("api/values/{id}")] public ActionResult Details(int id) { return Content("Value: " + id); } } }

Quiz šŸŽÆ

Quick Quiz
Question 1 of 1

What does the `[Route]` attribute do in ASP .NET?

That's it for today! Attribute Routing is a powerful tool in your ASP .NET arsenal. In the next lesson, we'll explore how to create custom attributes and use them to handle specific routing requirements. Stay tuned! šŸš€