Asp.net MVC routing using Attributes

AttributeRouting is an open source Nuget package which allows to specify routes using attributes in your controllers and actions.

Top Features

  1. Decorate your actions with GET, POST, PUT, DELETE, and verb agnostic attributes.
  2. Map multiple routes to a single action, ordering them with an Order property.
  3. Specify route defaults and constraints inline and also using attributes.
  4. Specify optional params inline with a token before or after the parameter name.
  5. Specify the route name for supporting named routes.
  6. Define MVC areas on a controller or base controller.
  7. Group or nest your routes together using route prefixes applied to a controller or base controller.
  8. Support legacy URLs.
  9. Set the precedence of routes among the routes defined for an action, within a controller, and among controllers and base controllers.
  10. Generate lowercase outbound URLs  automatically.
  11. Define your own custom route conventions and apply them on a controller to generate the routes for actions within the controller without boilerplate attributes (think RESTful style).
  12. Debug your routes using a supplied HttpHandler.

Installing

In Nuget package manager console type the below line :


Install-Package AttributeRouting

Usage


public class ProductsController : Controller
{
[GET("Products")]
public ActionResult Index()
{
return View();
}

[GET("Products/New")]
public ActionResult New()
{
return View();
}

[POST("Products")]
public ActionResult Create()
{
return RedirectToAction("Index");
}

[GET("Resources/{id}")]
public ActionResult Show(int id)
{
return View();
}

[GET("Products/{id}/Edit")]
public ActionResult Edit(int id)
{
return View();
}

[PUT("Resources/{id}")]
public ActionResult Update(int id)
{
return RedirectToAction("Show");
}

[GET("Products/{id}/Delete")]
public ActionResult Delete(int id)
{
return View();
}

[DELETE("Products/{id}")]
public ActionResult Destroy(int id)
{
return RedirectToAction("Index");
}
}

in order to make the above code working you need to have the RoutingAttributes library to find them and create corresponding routes in routes table. make the below changes in the global.asax


public static void RegisterRoutes(RouteCollection routes)
{
routes.IgnoreRoute("{resource}.axd/{*pathInfo}");

routes.MapAttributeRoutes();
}

if you have installed this package via Nuget, you will have a file in app_start folder that will call this registration hook automatically

ASP.Net MVC Areas

Introduction

Areas allows you to separate your large projects into more distinct partitions. Its like having a sub project inside your main project. All your Controllers, Views and Models for a specific module will belong to one directory structure. To create an area simply right click on anywhere in your project in solution explorer and click on Add – > Area The area will create in a new ”Areas” Folder.

Once created you can see the all familiar folder structure like Controllers, Views and Models inside the newly created Area folder by name. you can also see a new AreaRegistration.cs starting with your area name. this class is derived from AreaRegistration class in System.Web.Mvc and contains one overridden property  AreaName and another overridden method RegisterArea. the property will return your Area name and the method registered the routes we define under the area. creating an area also calls AreaRegistration.RegisterAllAreas in Application_start method in Global.asax.cs.

Content of AdminAreaRegistration.cs


public class AdminAreaRegistration : AreaRegistration
{
public override string AreaName
{
get
{
return "Admin";
}
}

public override void RegisterArea(AreaRegistrationContext context)
{
context.MapRoute("Admin_default",
"Admin/{controller}/{action}/{id}",
new { action = "Index", id = UrlParameter.Optional });
}
}

Content Of Application_Start


protected void Application_Start()
{
AreaRegistration.RegisterAllAreas();

RegisterGlobalFilters(GlobalFilters.Filters);
RegisterRoutes(RouteTable.Routes);
}

Microsoft released Asp.Net MVC3 tools update

Microsoft has released a new update for Asp.Net MVC3. This update won’t update the MVC3 runtime, it only updates the tooling for MVC3 and this also installs NuGet v1.2 (NuGet is a Visual Studio extension that makes it easy to add, remove, and update libraries and tools in Visual Studio projects that use the .NET Framework). You can download the MVC3 tools update from here.

Changes in ASP.NET MVC 3 Tools Update

  • New Intranet Project Template for Windows Authentication.
  • HTML 5 checkbox to enable HTML 5 versions of project templates.
  • “Add Controller” dialog box can now scaffold controllers with views and data access code.
  • Project templates now include Modernizr 1.7 (Modernizr adds classes to the <html> element which allow you to target specific browser functionality in your stylesheet without actually need to write any JavaScript to use it)
  • Project templates include updated versions of jQuery (jQuery 1.5.1), jQuery UI (jQuery UI 1.8.11), and jQuery Validation (jQuery Validation 1.8) .
  • Project templates now include ADO.NET Entity Framework 4.1 as a pre-installed NuGet package.
  • Project templates include JavaScript libraries as pre-installed NuGet packages.