C# 无法访问ASP.NET MVC中创建的Post API
我有一个用于注册用户的API 这是Api的代码C# 无法访问ASP.NET MVC中创建的Post API,c#,asp.net-mvc,asp.net-web-api,C#,Asp.net Mvc,Asp.net Web Api,我有一个用于注册用户的API 这是Api的代码 public class AccountsController : ApiController { [HttpPost] [ActionName("Register")] public HttpResponseMessage Register(string SystemKey, string IPAddress, [FromUri]DomainModels.InstaUser.User user) {
public class AccountsController : ApiController
{
[HttpPost]
[ActionName("Register")]
public HttpResponseMessage Register(string SystemKey, string IPAddress, [FromUri]DomainModels.InstaUser.User user)
{
SessionUser _user = new SessionUser(user);
if (!ValidateSystemKey(SystemKey))
{
return Request.CreateErrorResponse(HttpStatusCode.Unauthorized, "Invalid Security Key");
}
else if (string.IsNullOrWhiteSpace(IPAddress))
{
return Request.CreateErrorResponse(HttpStatusCode.Unauthorized, "Invalid IP Address");
}
else if (Models.User.AlreadyExists(user.Username))
{
return Request.CreateErrorResponse(HttpStatusCode.Conflict, "Username already registered");
}
else if (Models.User.AlreadyExistsPhone(user.Phone))
{
return Request.CreateErrorResponse(HttpStatusCode.Conflict, "Phone Number already registered");
}
else if (DomainModels.InstaUser.User.Register(user, IPAddress))
{
CreateTokens(_user, IPAddress);
return Request.CreateResponse(HttpStatusCode.OK, user);
}
else
{
return Request.CreateErrorResponse(HttpStatusCode.InternalServerError, "Unable to register user due to techincal issues");
}
}
}
这是路线
///{SystemKey}/{IPAddress}/{Username}/{Password}/{Name}/{Email}/{Phone}/{Type}
config.Routes.MapHttpRoute(
name: "Register",
routeTemplate: "api/Accounts/Register",
defaults: new { controller = "Accounts", action = "Register", SystemKey = RouteParameter.Optional, IPAddress = RouteParameter.Optional, Username = RouteParameter.Optional, Password = RouteParameter.Optional, Name = RouteParameter.Optional, Email = RouteParameter.Optional, Phone = RouteParameter.Optional, Type = RouteParameter.Optional }
);
我还尝试添加和删除可选参数
这就是我试图击中它的方式
Dictionary<string, string> Params = new Dictionary<string, string>();
Params.Add("SystemKey", ServerConfig.SystemKey);
Params = Params.Union(Common.ConvertToDictionary(data)).ToDictionary(x=>x.Key, x =>x.Value); // data is a SessionUser object that i am taking input
using (HttpClient client = new HttpClient())
{
var parameters = new FormUrlEncodedContent(nameValueCollection: Params);
string Path = ServerConfig.server_path + "api/Accounts/Register";
var response = client.PostAsJsonAsync(Path, parameters).Result;
//var response = client.PostAsync(Path, parameters).Result; both give same response of 404
}
Dictionary Params=newdictionary();
Params.Add(“SystemKey”,ServerConfig.SystemKey);
Params=Params.Union(Common.ConvertToDictionary(data)).ToDictionary(x=>x.Key,x=>x.Value);//数据是我正在接受输入的SessionUser对象
使用(HttpClient=new HttpClient())
{
var参数=新FormUrlEncodedContent(nameValueCollection:Params);
string Path=ServerConfig.server_Path+“api/Accounts/Register”;
var response=client.PostAsJsonAsync(路径、参数).Result;
//var response=client.PostAsync(路径、参数).Result;两者给出相同的404响应
}
未命中API,响应包含404未找到
我能用邮递员点击api。但是c#代码没有触及api。路径的值是什么?
Path
?string Path=ServerConfig.server#u Path+“api/Accounts/Register”;它的路径是什么?字符串Path=ServerConfig.server\u Path+“api/Accounts/Register”;它的