Danny Rancher
Danny Rancher

Reputation: 2005

What is the difference between AttributeRoutingConfig and RouteConfig in MVC5?

To understand the code behind the AttributeRoutingConfig I'm trying to recreate it using RouteConfig

AttributeRoutingConfig.cs

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Routing;
using AttributeRouting.Web.Mvc;

[assembly: WebActivator.PreApplicationStartMethod(typeof(SimplestAuth.AttributeRoutingConfig), "Start")]

namespace xyz
{
    public static class AttributeRoutingConfig
    {
        public static void RegisterRoutes(RouteCollection routes) 
        {                
            routes.MapAttributeRoutes();
        }

        public static void Start() 
        {
            RegisterRoutes(RouteTable.Routes);
        }
    }
}

RoutingConfig.cs

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using System.Web.Routing;

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

            routes.MapRoute(
                name: "Default",
                url: "{controller}/{action}/{id}",
                defaults: new { controller = "Home", action = "login", id = UrlParameter.Optional }
            );
        }
    }
}

I'm looking for information to understand the differences and similarities between the two techniques. Thank you.

Upvotes: 0

Views: 315

Answers (1)

Khalid Abuhakmeh
Khalid Abuhakmeh

Reputation: 10839

They both do essentially the same thing:

They register your routes to the RouteTable.

  • The first code sample you have above utilizes AttributeRouting so it does an assembly scan.
  • The second code sample you have utilizes the RouteTable directly, so it is a much more manual looking process.

Hope that helps.

Upvotes: 1

Related Questions