简体   繁体   English

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

[英]How to create common method for web api Url in asp.net MVC4

I've started using Web Api.我已经开始使用 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.我尝试为所有 web api 请求创建一个主方法,例如在下面的快照中,方法名称 GetMenu() 和参数将是 userpkid。

快照 1

Now,I will try to create Common Method for web api.现在,我将尝试为 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.当请求来自 web api 时,它们将方法名称和参数分开,而不是动态调用任何方法名称并传递参数。 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..那么我怎样才能做到这一点..

快照 2

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 . 默认情况下,在 Microsoft Web Api 中,查询字符串参数名称必须与方法的参数变量名称匹配 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"

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM