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 Tech Blog - https://www.code-sample.com/
My Books - Book 1 and Book 2

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