I am learning web api. just gone through few articles and found attribute routing can be done different way.
- https://www.codeproject.com/Articles/774807/Attribute-Routing-in-ASP-NET-MVC-WebAPI
- http://www.c-sharpcorner.com/UploadFile/b1df45/web-api-route-and-route-prefix-part-2/
- https://www.codeproject.com/Articles/999691/RESTful-Day-sharp-Custom-URL-Re-Writing-Routing-us
See the code used:
[RoutePrefix("Movie")]
public class HomeController : Controller
{
//Route: Movie/Index
[Route]
public ActionResult Index()
{
ViewBag.Message = "You are in Home Index";
return View();
}
//Route: NewRoute/About
[Route("~/NewRoute/About")]
public ActionResult About()
{
ViewBag.Message = "You successfully reached NEWRoute/About route";
return View();
}
}
in above example the author use Route attribute to define route for action.
see this one again
[GET("productid/{id?}")]
public HttpResponseMessage Get(int id)
{
var product = _productServices.GetProductById(id);
if (product != null)
return Request.CreateResponse(HttpStatusCode.OK, product);
return Request.CreateErrorResponse(HttpStatusCode.NotFound, "No product found for this id");
}
Here the author do not use route attribute rather use http verbs to define route.
So tell me which approach is right?
Another question that by attribute routine we can give different name to action then when one should use action name attribute to give different name to action?
When we can change action name by attribute routing then why should one use action name attribute to give different name to action?