2

I am learning web api. just gone through few articles and found attribute routing can be done different way.

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?

1
  • You can also use [HttpGet] attribute to define http verb. I use that and [Route()] because I have action method name in routing, imo that's provides the best readability but does clutter. Commented Jun 8, 2017 at 15:12

3 Answers 3

1

[Route] exposes an action to the outside world. It takes the action name (method name) as the name for the route. It can use the RoutePrefix or controller name to construct the URL.

If you provide a value to [Route(template)], that (relative) URL is used to construct the URL the action is available.

Since routes are by default only accessible through GET, you can use HttpGet(template) and Route the same way. If you use Route together with HttpGet and HttpPost for example, that route will be available through those verbs.

So you can mix around a little, depending on your needs and preferences.

Sign up to request clarification or add additional context in comments.

2 Comments

thanks for reply but sorry i did not understand what you said like Since routes are by default only accessible through GET, you can use HttpGet(template) and Route the same way. If you use Route together with HttpGet and HttpPost for example, that route will be available through those verbs. so would make these point more clear with example that what you try to say. thanks
i have seen some time people use multiple route for same action......when one should follow these approach ? just check this url codeproject.com/Articles/1005485/… there you must notice multiple route has been used for single action....why?
0

I believe [Route], RoutePrefix are those attributes which help us define the URL which we expose to the client browser.

Answer from

Patrick Hofman is well explanatory.

To indicate clearly how any written method works, there are HTTP verbs provided. Recently, I developed small Web API solution where I tried and tested Routing scenario with multiple attributes. Here they are:

[ActionName("GetIndex")]
[HttpGet]
public HttpResponseMessage IndexIdById(string id)
{
... other relative code here ...
var response = new HttpResponseMessage(HttpStatusCode.OK);
return response;
}

If the method name is written without any Get prefix (as per above example) it would give precise indication to specify [HttpGet] verb to show what the method is doing. Here this method is a Get method.

Also with such method name like above, it would be nicer to provide an alias name. [ActionName("GetIndex")]

To support such method with route configuration, we can provide route which maps Action name with other portion of the URL in WebApiConfig.cs. Example like below :-

config.Routes.MapHttpRoute(
name: "GetIndex",
routeTemplate: "api/{controller}/{action}/{id}",
defaults: null
);

I appreciate others advise or improvements to this answer. As this strategy to configure my Web API worked for me.

2 Comments

thanks for the reply. tell me what will be the url for this action IndexIdById ?
The URL should be :- when running on localhost. localhost:[portnumber]/api/services/getindex/10000 where it reflects the configuration in :- services = {controller} getindex = {action} - this is an alias I set as [ActionName("GetIndex")] above action 10000 = {id}
0
routes.MapRoute(
  "DesktopLogin",
  "{controller}/account/Function1",
  new { controller = "My", action = "Login" }
 );
 routes.MapRoute(
"NewDeskTopLogin",
 "{controller}/account/Function1",
 new { controller = "My", action = "Login" }
 );

 ///////////RouteAttribute and HttpPost together on same action


[Route("DesktopLogin")]
public ActionResult Function1(LoginModel model)
{
  return View("~/Views/Account/Login.cshtml");
 }

[Route("NewDeskTopLogin")]
[HttpPost]
public ActionResult Function1(LoginModel model)
{
  return View("~/Views/Account/Login.cshtml");
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.