在我的WEB API中,我收到500错误。
错误状态'内部服务器错误'。错误代码500
public class VendorManagementController : ApiController
{
private readonly IVendorRespository _vendorRespository;
public VendorManagementController()
{
_vendorRespository = new VendorRespository();
}
[Microsoft.AspNet.Mvc.HttpGet]
public dynamic GetVendorById(int pkey)
{
Vendor vendor = _vendorRespository.GetVendor(pkey);
return vendor;
}
// GET
[Microsoft.AspNet.Mvc.HttpGet]
public dynamic GetVendors(string sidx, string sord, int page, int rows)
{
var vendors = _vendorRespository.GetAllVendors();
var pageIndex = Convert.ToInt32(page) - 1;
var pageSize = rows;
var totalRecords = vendors.Count();
var totalPages = (int) Math.Ceiling((float) totalRecords/(float) pageSize);
vendors = vendors.Skip(pageIndex*pageSize).Take(pageSize);
return new
{
total = totalPages,
page = page,
records = totalRecords,
rows = (from vendor in vendors
select new
{
cell = new string[]
{
vendor.pkey.ToString(),
vendor.Company,
vendor.ContactName,
vendor.ContactPhone,
vendor.UserName,
Encoding.UTF8.GetString(vendor.UserKey),
vendor.Active.ToString(),
vendor.FacilityId.ToString(),
vendor.ClientID.ToString(),
vendor.PhotoURL,
vendor.PushToGP.ToString()
}
}).ToArray()
};
}
// POST
[System.Web.Http.HttpPost]
public HttpResponseMessage PostVendor(Vendor item)
{
var password = CreatePassword(25);
item.UserKey = Encoding.UTF8.GetBytes(password);
_vendorRespository.AddVendor(item);
var response = Request.CreateResponse<Vendor>(HttpStatusCode.Created, item);
string uri = Url.Link("/VendorManagement/GetVendorById", new {id = item.pkey});
response.Headers.Location = new Uri(uri);
return response;
}
private string CreatePassword(int length)
{
const string valid = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890";
StringBuilder res = new StringBuilder();
Random rnd = new Random();
while (0 < length--)
{
res.Append(valid[rnd.Next(valid.Length)]);
}
return res.ToString();
}
}
它发生在POST中的这一行var response = Request.CreateResponse<Vendor>(HttpStatusCode.Created, item);
。
例外是&#34;值不能为空。 参数名称:request&#34;。
为什么呢? item
不为空。
答案 0 :(得分:0)
自己找到答案。我使用的是asp.net 5.框架是DNX 1.0。似乎不太支持HttpResponseMessage
。