本文介绍了MVC4网络与多个参数API REST接口的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个名为LoginController中有一个Get方法用的签名控制器:

 公共字符串获取(字符串键,字符串code,字符串userid,字符串密码)

我想成为能够与类似的调用来调用它:

 的http://本地主机:1234 / API /登录/ KEYVALUE / ​​codeValue / UserValue / PasswordValue

我不能得到这个工作。如果我调用与呼叫:

<$p$p><$c$c>http://localhost:1234/api/Login?Key=KeyValue&$c$c=$c$cValueUserID=UserValue&Password=PasswordValue

调用成功。

我试着添加路由,如下面的Global.asax

  routes.MapHttpRoute(名称:登录,routeTemplate:API / {控制器} / {行动} / {主要} / {code} / {用户名} / {密码},
                默认:新{重点= UrlParameter.Optional,code = UrlParameter.Optional,用户名= UrlParameter.Optional,密码= UrlParameter.Optional});

  routes.MapHttpRoute(名称:登录,routeTemplate:API / {控制器} / {主要} / {code} / {用户名} / {}密码 ,
                默认:新{重点= UrlParameter.Optional,code = UrlParameter.Optional,用户名= UrlParameter.Optional,密码= UrlParameter.Optional});

这些似乎并不管用。我要去哪里错了或者是这甚至可能吗?我能做到这一点的RC版的WebAPI与MVC3。


解决方案

看来你缺少行动,你的要求( / API /登录/ KEYVALUE / ​​codeValue / UserValue / PasswordValue )。
尝试 / API /登录/ GET / KEYVALUE / ​​codeValue / UserValue / PasswordValue 相反,如果你打算使用的第一个路径。

如果您希望能够调用它没有指定的动作,并默认为获取,你必须指定一个默认动作:

 默认:新{重点= UrlParameter.Optional,code = UrlParameter.Optional,用户名= UrlParameter.Optional,密码= UrlParameter.Optional,行动=GET}

我已经成功地尝试这种在ASP.NET MVC 4项目(的Visual Studio 2012 RC):

创建具有行动的LoginController:

 公共字符串获取(字符串键,字符串code,字符串userid,字符串密码)
{
    返回键+ code +用户名+密码;
}

和在Global.asax.cs中绘制路线:

  RouteTable.Routes.MapHttpRoute(NULL,API / {控制器} / {主要} / {code} / {用户名} / {}密码,
            新{重点= UrlParameter.Optional,code = UrlParameter.Optional,用户名= UrlParameter.Optional,密码= UrlParameter.Optional,行动=GET});

如果它不为你工作,也许是另一条路线正赶上请求或者路由没有被注册。

I have a controller named LoginController with a Get method with a signature of:

public string Get(string Key, string Code, string UserID, string Password)

I want to be be able to invoke it with a call similar to:

http://localhost:1234/api/Login/KeyValue/CodeValue/UserValue/PasswordValue

I cannot get this to work. If I invoke the call with:

http://localhost:1234/api/Login?Key=KeyValue&Code=CodeValueUserID=UserValue&Password=PasswordValue

The call is successful.

I've tried adding routes such as below to Global.asax

 routes.MapHttpRoute(name: "Login", routeTemplate: "api/{controller}/{action}/{Key}/{Code}/{UserID}/{Password}",
                defaults: new { Key = UrlParameter.Optional, Code = UrlParameter.Optional, UserID = UrlParameter.Optional, Password = UrlParameter.Optional });

or

 routes.MapHttpRoute(name: "Login", routeTemplate: "api/{controller}/{Key}/{Code}/{UserID}/{Password}",
                defaults: new { Key = UrlParameter.Optional, Code = UrlParameter.Optional, UserID = UrlParameter.Optional, Password = UrlParameter.Optional });

These do not seem to work. Where am I going wrong or is this even possible? I was able to do this in the RC version of WebApi with MVC3.

解决方案

It seems you are missing the action in your request (/api/Login/KeyValue/CodeValue/UserValue/PasswordValue).Try /api/Login/Get/KeyValue/CodeValue/UserValue/PasswordValue instead, if you intend to use the first route.

If you want to be able to call it without the action specified and default to "Get", you have to specify a default action:

defaults: new { Key = UrlParameter.Optional, Code = UrlParameter.Optional, UserID = UrlParameter.Optional, Password = UrlParameter.Optional, Action = "Get" }

I have successfully tried this in an ASP.NET MVC 4 project (Visual Studio 2012 RC):

Creating a LoginController with action:

public string Get(string Key, string Code, string UserID, string Password)
{
    return Key + Code + UserID + Password;
}

And mapping the route in Global.asax.cs:

 RouteTable.Routes.MapHttpRoute(null, "api/{controller}/{Key}/{Code}/{UserID}/{Password}",
            new { Key = UrlParameter.Optional, Code = UrlParameter.Optional, UserID = UrlParameter.Optional, Password = UrlParameter.Optional, Action = "Get"});

If it is not working for you, maybe another route is catching the request or the route is not being registered.

这篇关于MVC4网络与多个参数API REST接口的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-29 01:29