我是 asp.net 的新手,遇到了一个小问题,我在 MVC 项目中创建了一个新的 webapi 控制器,当我尝试从这个控制器获得响应时,我得到 404 错误:s
这是使用的控制器代码:
public class SampleController : ApiController
{
[HttpPost]
public string Auth(string number, string password)
{
....
}
[HttpPost]
public string SendSms(string smsTo, string content)
{
....
}
}
WebApiConfig:
public static class WebApiConfig
{
public static void Register(HttpConfiguration config)
{
config.Routes.MapHttpRoute(
name: "DefaultApi",
routeTemplate: "api/{controller}/{id}",
defaults: new { id = RouteParameter.Optional }
);
}
}
这里是代码 c#:
using (var client = new HttpClient())
{
client.BaseAddress = new Uri("http://localhost:66893");
var content = new FormUrlEncodedContent(new[]
{
new KeyValuePair<string, string>("number", "login"),
new KeyValuePair<string, string>("password", "password")
});
var result = client.PostAsync("/api/sms/auth", content).Result;
string resultContent = result.Content.ReadAsStringAsync().Result;
Console.WriteLine(resultContent);
}
当我在 XHR 海报中尝试 localhost:66893/api/sms/auth?number=xxx&password=xxx 时,我得到了结果:s
请帮助谢谢