AttributeRouting in Web API 2 MVC 5

AttributeRouting in Web API 2 MVC 5

The code sample for Web API Routing as given below.
namespace demoRoutes
{
    public static class WebApiConfig
    {
        public static void Register(HttpConfiguration config)
        {
            // Web API configuration and services

            // Web API routes
            config.MapHttpAttributeRoutes();

            config.Routes.MapHttpRoute(
                name: "DefaultApiWithAction",
                routeTemplate: "api/{controller}/{action}/{id}",
                defaults: new { id = RouteParameter.Optional }
            );

            config.Routes.MapHttpRoute(
                name: "DefaultApi",
                routeTemplate: "api/{controller}/{id}",
                defaults: new { id = RouteParameter.Optional }
            );            
        }
    }
}
The code sample for MVC 5 Routing as given below.
namespace demoRoutes
{
    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 = "Index", id = UrlParameter.Optional }
            );
        }
    }
}

The Attribute based routing in MVC 5 as given below.

[RoutePrefix("API/Alert")]
public class AlertController : ApiController
{
    Mapper mapper = new Mapper();

    /// This method is used to GetReceipe collection.
    [Route("GetReceipe")]
    public IEnumerable GetReceipe()
    {
        DALReceipe objDalReceipe = new DALReceipe();
        ICollection resceipe = mapper.MapDALReceipes2BAL(objDalReceipe.getReceipe());
        return resceipe;
    }
}
ANIL SINGH

Anil Singh is an author, tech blogger, and software programmer. Book writing, tech blogging is something do extra and Anil love doing it. For more detail, kindly refer to this link..
My Blogs - https://code-sample.com and https://code-sample.xyz
My Book1 - BEST SELLING ANGULAR BOOK (INCLUDING ALL VERSIONS 2, 4, 5, 6, 7)
My Book2 - ANGULAR 2 INTERVIEW QUESTIONS BOOK - Both Books are Available on WorldWide.

www.code-sample.com/. Powered by Blogger.