gpt4 book ai didi

c# - MVC4 WebAPI 找到多个与请求匹配的操作

转载 作者:行者123 更新时间:2023-11-30 16:48:41 25 4
gpt4 key购买 nike

我已经搜索了一段时间,但我找到的帮助都没有解决我的问题。我有一个 MVC4 WebAPI 项目,但遇到“发现多个与请求匹配的操作...”问题。

这是我的 Controller :

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 对我来说是不可能的。有什么问题吗?

最佳答案

您不需要创建不同的路线。您的所有三个路线都可以用这条路线处理。

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 });
}
}

关于c# - MVC4 WebAPI 找到多个与请求匹配的操作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37862080/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com