1

I've started using Web Api. I try to create one master method for all web api request for example in below Snapshot there method name GetMenu() and parameter would be userpkid.

snapshot 1

Now,I will try to create Common Method for web api. when request Come from web api than they separate method name and parameter than call the whatever method name dynamically and pass parameter. For Example request comes For menu Control than they go in menu control for whatever method name and parameter if request come for country control than go in country control for whatever method name and parameter. so how can i achieve this..

Snapshot 2

Ravi Shah
  • 843
  • 12
  • 29
  • Add a sourceType param in you client's request header and extract it from HttpRequestHeaders in webapi and then segment your function based on the sourceType. You can make an enumeration for that purpose in your service api if you have a constant set of sourcetype. Then use switch case for segmentation your CommonWebApiMethod. SourceType can be your controls – Pushpendra May 30 '16 at 13:14
  • Hi, from web api url i differentiate controller name,method name and parameter no how to call this method for whatever that specific controller and specific parameter.. – Ravi Shah May 31 '16 at 10:18

1 Answers1

1

The solution depends on whether the parameter name is important. By default within Microsoft Web Api, the query string parameter name must match the parameter variable name of the method. For example:

If the url is

"api/MenuData/GetMenu?UserPKId=1"

then the controller method must have the following parameter list

public MyModel CommonWebApiMethod(string MethodName, string UserPKId)

Unimportant parameter name

Configure the route:

public static class WebApiConfig
{
    public static void Register(HttpConfiguration config)
    {
        // Web API configuration and services

        // Web API routes
        config.MapHttpAttributeRoutes();

        config.Routes.MapHttpRoute(
            name: "MethodName",
            routeTemplate: "api/MenuData/{MethodName}",
            defaults: new { controller = "Common", action = "CommonWebApiMethod" }
        );
    }
}

Controller:

public class CommonController : ApiController
{
    [HttpPost]
    public MyModel CommonWebApiMethod(string MethodName, string parameter)
    {
        return new MyModel { MethodName = MethodName, Parameter = parameter };
    }
}

Calling url:

"api/MenuData/GetMenu?parameter=1"

Important parameter name

public static class WebApiConfig
{
    public static void Register(HttpConfiguration config)
    {
        // Web API configuration and services

        // Web API routes
        config.MapHttpAttributeRoutes();

        config.Routes.MapHttpRoute(
            name: "ParameterName",
            routeTemplate: "api/MenuData/{MethodName}/{parameterName}",
            defaults: new { controller = "Common", action = "CommonWebApiMethod" }
            );
    }
}

Controller:

public class CommonController : ApiController
{
    [HttpPost]
    public MyModel CommonWebApiMethod(string MethodName, string parameterName, string parameter)
    {
        return new MyModel { MethodName = MethodName, Parameter = parameter };
    }
}

Calling url:

"api/MenuData/GetMenu/UserPKId?parameter=1"
Community
  • 1
  • 1
poppertech
  • 1,286
  • 2
  • 9
  • 17