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

2
  • 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 Commented May 30, 2016 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.. Commented May 31, 2016 at 10:18

1 Answer 1

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"
Sign up to request clarification or add additional context in comments.

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.