我已经搜索了一段时间,但我找到的帮助都没有解决我的问题。 我有一个MVC4 WebAPI项目,我得到“发现符合请求的多个操作......”问题。
这是我的控制器:
public class DataEntryController : ApiController
{
[HttpPost]
[ActionName("GetMessageId")]
public HttpResponseMessage GetMessageId(HttpRequestMessage request)
{
}
[HttpPost]
[ActionName("RequestXmlDataEntry")]
public HttpResponseMessage RequestXmlDataEntry(HttpRequestMessage request)
{
}
[HttpPost]
[ActionName("SendConfirmationXmlDataEntry")]
public HttpResponseMessage SendConfirmationXmlDataEntry(HttpRequestMessage request)
{
}
[HttpPost]
[ActionName("SendEvent")]
public HttpResponseMessage SendEvent(HttpRequestMessage request)
{
}
}
这是我的路线:
public static class WebApiConfig
{
public static void Register(HttpConfiguration config)
{
config.Routes.MapHttpRoute(
name: "DefaultApi",
routeTemplate: "api/{controller}/{id}",
defaults: new { id = RouteParameter.Optional }
);
config.Routes.MapHttpRoute(
name: "API Default 2",
routeTemplate: "api/{controller}/{action}");
config.Routes.MapHttpRoute(
name: "API Default",
routeTemplate: "api/{controller}/{action}/{id}",
defaults: new { id = RouteParameter.Optional });
}
}
以下是我的请求的样子:
public static string PostRequestToRestMethod(string url, string data, IWebProxy proxy, int timeout)
{
byte[] byteArray = Encoding.UTF8.GetBytes(data);
string responseFromServer = string.Empty;
HttpWebRequest webrequest = (HttpWebRequest)WebRequest.Create(url);
webrequest.Method = "POST";
webrequest.ContentType = "text/xml";
webrequest.ContentLength = byteArray.Length;
webrequest.Timeout = timeout;
if (proxy != null)
webrequest.Proxy = proxy;
var dataStream = webrequest.GetRequestStream();
dataStream.Write(byteArray, 0, byteArray.Length);
dataStream.Close();
HttpWebResponse webresponse = (HttpWebResponse)webrequest.GetResponse();
using (StreamReader responseStream = new StreamReader(webresponse.GetResponseStream(), Encoding.UTF8))
{
responseFromServer = responseStream.ReadToEnd();
}
webresponse.Close();
return responseFromServer;
}
我正在使用.Net Framework 4.0,因此WebAPI 2对我来说是不可能的。有什么难事吗?
答案 0 :(得分:2)
您不需要创建不同的路径。您可以使用这条路线处理所有三条路线。
public static class WebApiConfig
{
public static void Register(HttpConfiguration config)
{
config.Routes.MapHttpRoute(
name: "API Default",
routeTemplate: "api/{controller}/{action}/{id}",
defaults: new { id = RouteParameter.Optional });
}
}