Json 如何在asp.net MVC4中创建web api Url的通用方法

Json 如何在asp.net MVC4中创建web api Url的通用方法,json,asp.net-mvc-4,c#-4.0,asp.net-web-api,Json,Asp.net Mvc 4,C# 4.0,Asp.net Web Api,我已经开始使用Web Api。我尝试为所有web api请求创建一个主方法,例如在下面的快照中,方法名为GetMenu(),参数为userpkid 现在,我将尝试创建web api的通用方法。当请求来自WebAPI时,它们将方法名和参数分开,然后动态调用任意方法名并传递参数。例如,如果请求来自国家/地区控制,则对于菜单控制,而不是对于任何方法名称和参数,它们进入菜单控制;如果请求来自国家/地区控制,则对于任何方法名称和参数,它们进入国家/地区控制。那么我如何才能做到这一点 解决方案取决于参数名

我已经开始使用Web Api。我尝试为所有web api请求创建一个主方法,例如在下面的快照中,方法名为GetMenu(),参数为userpkid

现在,我将尝试创建web api的通用方法。当请求来自WebAPI时,它们将方法名和参数分开,然后动态调用任意方法名并传递参数。例如,如果请求来自国家/地区控制,则对于菜单控制,而不是对于任何方法名称和参数,它们进入菜单控制;如果请求来自国家/地区控制,则对于任何方法名称和参数,它们进入国家/地区控制。那么我如何才能做到这一点


解决方案取决于参数名称是否重要。例如:

如果url是

"api/MenuData/GetMenu?UserPKId=1"
那么控制器方法必须具有以下参数列表

public MyModel CommonWebApiMethod(string MethodName, string UserPKId)
不重要的参数名称

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" }
            );
    }
}
配置路由:

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" }
        );
    }
}
控制器:

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

"api/MenuData/GetMenu?parameter=1"
重要参数名称

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" }
            );
    }
}
控制器:

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

"api/MenuData/GetMenu/UserPKId?parameter=1"

在客户端的请求头中添加sourceType参数,并从webapi中的HttpRequestHeaders中提取该参数,然后根据sourceType对函数进行分段。如果有一组常量sourcetype,则可以在服务api中为此目的进行枚举。然后使用switch case分割您的commonwebapi方法。SourceType可以是您的controlsHi,从web api url我区分控制器名称、方法名称和参数没有如何为特定控制器和特定参数调用此方法。。