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

Hey! I'm Anil Singh. I author this blog. I'm Active Blogger, Programmer. I love learning new technologies, programming, blogging and participating the forum discussions more...
My Blogs - http://www.code-sample.com and http://www.code-sample.xyz
My Books - Buy Books Online at Best Prices

You Might Also Like
Post a Comment
www.code-sample.com/. Powered by Blogger.