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!
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.
Let's create a simple example to illustrate Attribute Routing.
AttributeRoutingExample.Right-click on the Controllers folder and select Add > Controller. Name the new controller ValuesController.
In the ValuesController.cs file, replace the default code with the following:
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.
Run the application and navigate to http://localhost:<port>/api/values in your browser. You should see the view from the Index action.
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".
You can create custom attributes to handle specific routing requirements. Let's create a custom attribute for handling JSON responses.
Right-click on the project and select Add > New Item.
Choose Class and name it JsonOutputAttribute.
Replace the default code with the following:
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.
ValuesController.cs file to use the custom attribute: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);
}
}
}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! š