Click here to Skip to main content
13,798,571 members
Rate this:
 
Please Sign up or sign in to vote.
See more:
Hi,

how to execute MVC web api method with different name but type is GET and POST. i can't able to execute. it shows error. here is my code.

webapiconfig.cs

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



Employee controller :

[HttpGet]
      public string Test1()
      {
          return "this is a string";
      }

      [HttpGet]
      public string Test2()
      {
          return "this is a another string";
      }
   [HttpPost]
        public string Test3()
        {
            return "this is a another post string";
        }


i want to execute all this method in different scenario. how to do it?
Posted 26-Nov-14 4:34am
Rate this: bad
 
good
Please Sign up or sign in to vote.

Solution 1

Add two or more route to the config. So you call api method by using name like below
config.Routes.MapHttpRoute(
            name: "ApiByName",
            routeTemplate: "api/{controller}/{action}/{name}",
            defaults: null,
            constraints: new { name = @"^[a-z]+$" }
           );
          config.Routes.MapHttpRoute(
              name: "ApiByAction",
              routeTemplate: "api/{controller}/{action}",
              defaults: new { action = "Get" }
          );
          config.Routes.MapHttpRoute(
              name: "DefaultApi",
              routeTemplate: "api/{controller}/{id}",
              defaults: new { id = RouteParameter.Optional }
          );


Hope this helps
  Permalink  
Rate this: bad
 
good
Please Sign up or sign in to vote.

Solution 2

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


You didn't mention controller.
[httpget],[httppost] are get differentiate with parameter.
You are not suppose to register all the route. Only default one is required.
Later application manage by clicking on different links.
  Permalink  
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
Top Experts
Last 24hrsThis month


Advertise | Privacy | Cookies | Terms of Service
Web01 | 2.8.181207.3 | Last Updated 3 Dec 2014
Copyright © CodeProject, 1999-2018
All Rights Reserved.
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100