我已按此配置路由:
config.Routes.MapHttpRoute(
name: "Sales",
routeTemplate: "api/{Sales}/{jsonresponce}",
defaults: new { controller = "Sales", action = "Postsomething" }
);
config.Routes.MapHttpRoute(
name: "User",
routeTemplate: "api/{User}/{GetDetails}",
defaults: new { controller = "User", action = "GetDetails" }
);
这是我的UserController:
public class UserController : ApiController
{
userservice objservice = new userservice();
[HttpGet]
public CustDetails GetDetails(string Username, string Password, string BillingFeedID)
{
CustDetails model = new CustDetails();
//checking for encrypted password
model.UserName = Username;
model.Password = Password;
model.BillingFeedID = BillingFeedID;
model = objservice.Login(model);
//taking merchant configuration data
var data = objservice.Getcustomerconfig(model.MerchantID, BillingFeedID);
model.LastPosBillID = data.LastPosBillID;
model.LastTimeStamp = data.LastTimeStamp;
model.SyncStatus = data.SyncStatus;
model.SynsTimeInterval = data.SynsTimeInterval;
model.DataSorce = data.DataSorce;
model.DataAuthentication = data.DataAuthentication;
model.DataBaseQuery = data.DataBaseQuery;
return model;
}
}
我还有一个SalesController:
public class SalesController : ApiController
{
[HttpPost]
public async Task<HttpResponseMessage> PostSomething()
{
StringBuilder sb = new StringBuilder();
try
{
string jsonData = await Request.Content.ReadAsStringAsync();
// dynamic dataList = JArray.Parse(jsonData);
if (File.Exists(@"C:\MCarrots\Umairbills\Umairbills.json"))
File.Delete(@"C:\MCarrots\Umairbills\Umairbills.json");
File.Create(@"C:\MCarrots\Umairbills\Umairbills.json").Close();
File.WriteAllText(@"C:\MCarrots\Umairbills\Umairbills.json", jsonData);
return Request.CreateResponse(HttpStatusCode.OK, "OK");
}
catch (Exception ex)
{
File.WriteAllText(@"C:\MCarrots\mcarrots\Umairbills.json", ex.ToString());
return Request.CreateResponse(HttpStatusCode.NoContent, ex.ToString());
}
}
当我尝试使用此url调用GetUserDetails操作时:
http://localhost:42945/api/User/GetDetails?Username=kay001&Password=kay501&BillingFeedID=KF1
抛出这个错误:
“消息”:“请求的资源不支持http方法”
但是SalesController中的POST方法正在运行。
答案 0 :(得分:3)
您的路线模板似乎已关闭。我认为他们应该是:
config.Routes.MapHttpRoute(
name: "Sales",
routeTemplate: "api/Sales/{action}",
defaults: new { controller = "Sales", action = "Postsomething" }
);
config.Routes.MapHttpRoute(
name: "User",
routeTemplate: "api/User/{action}",
defaults: new { controller = "User", action = "GetDetails" }
);
我更改了路径模板,以便控制器名称基本上是硬编码的,并且操作是占位符。在这种情况下,可以省略该操作,默认为GetDetails。