我有几个公开GetByID(int someInt)
方法的控制器/存储库。在大多数情况下,它们可以正常工作,但在一对情况下,它们会因404 (not found)
错误而失败。
例如,以下是来自测试客户端的代码,它调用两个REST方法:
GetById("inventories");
. . .
GetById("sitemapping");
private void GetById(string typeName)
{
// Had to add the "constant" (1) to prevent very bad things from happening (wherein the runtime has delusions of being a runaway train (DeserializeObject() call otherwise fails if only returning one "record"))
string uri = string.Format("{0}/{1}/1", typeName, numericUpDownId.Value);
Popul8TheGrid(uri);
}
private JArray GetRESTData(string uri)
{
var webRequest = (HttpWebRequest) WebRequest.Create(uri);
var webResponse = (HttpWebResponse) webRequest.GetResponse();
var reader = new StreamReader(webResponse.GetResponseStream());
string s = reader.ReadToEnd();
return JsonConvert.DeserializeObject<JArray>(s);
}
private void Popul8TheGrid(string uri)
{
try
{
dataGridView1.DataSource = GetRESTData(BASE_URI + uri);
}
catch (WebException webex)
{
MessageBox.Show(string.Format("Eek, a mousey-pooh! ({0})", webex.Message));
}
}
最终调用的Repository方法实际上是相同的:
库存(工作):
private readonly List<Inventory> inventories = new List<Inventory>();
. . .
public Inventory GetById(int ID)
{
return inventories.FirstOrDefault(p => p.Id == ID);
}
库存模型,其中可以看出“Id”是一个int:
public class Inventory
{
[Key]
public int Id { get; set; }
[Required]
public string InventoryName { get; set; }
. . .
}
Sitemapping(不工作/未找到(404):
private readonly List<SiteMapping> siteMappings = new List<SiteMapping>();
. . .
public SiteMapping GetById(int ID)
{
return siteMappings.FirstOrDefault(s => s.site_no == ID);
}
SiteMapping模型,其中可以看到“site_no”是一个int:
public class SiteMapping
{
public int site_no { get; set; }
public string location_num { get; set; }
}
为什么库存工作和SiteMapping失败?
对于基思:
URL(示例)是:
http://localhost:28642/api/sitemapping/3
- 和
http://localhost:28642/api/inventories/42
我可以直接在我的浏览器中添加第一个,并且它可以正常工作:它在Chrome中将预期的“记录”作为XML返回。只是从显示的代码中调用它失败了。
关于路由配置,我正在使用Castle Windsor,并在RepositoriesInstaller中使用这样的代码:
Component.For<IInventoryRepository>().ImplementedBy<InventoryRepository>().LifestylePerWebRequest(),
. . .
Component.For<ISiteMappingRepository>().ImplementedBy<SiteMappingRepository>().LifestylePerWebRequest(),
但这不是问题,因为相关方法调用工作正常(我应该提到这个:我可以调用其他SiteMapping方法,即Count和GetAll,一切都很好 - 它只是“GetByID() “那失败了。
Kiran,我不确定你的意思,但如果你的意思是我的控制器属性路由,那么它们就是:
工作(库存):
private readonly IInventoryRepository _inventoryRepository;
public InventoriesController(IInventoryRepository inventoryRepository)
{
if (inventoryRepository == null)
{
throw new ArgumentNullException("inventoryRepository");
}
_inventoryRepository = inventoryRepository;
}
[Route("api/Inventories/{ID:int}")]
public Inventory GetInventoryById(int ID)
{
return _inventoryRepository.GetById(ID);
}
非工作(仅限于GetByID()方法)(SiteMapping):
public readonly ISiteMappingRepository _sitemappingsRepository;
public SiteMappingController(ISiteMappingRepository sitemappingsRepository)
{
if (sitemappingsRepository == null)
{
throw new ArgumentNullException("SiteMappingController");
}
_sitemappingsRepository = sitemappingsRepository;
}
[Route("api/SiteMapping/{ID:int}")]
public SiteMapping GetSiteMappingById(int ID)
{
return _sitemappingsRepository.GetById(ID);
}