C# 为什么一些与工作调用几乎相同的REST调用会失败?
我有几个控制器/存储库,它们公开了一个C# 为什么一些与工作调用几乎相同的REST调用会失败?,c#,rest,castle-windsor,asp.net-web-api-routing,C#,Rest,Castle Windsor,Asp.net Web Api Routing,我有几个控制器/存储库,它们公开了一个GetByID(intsomeint)方法。在大多数情况下,它们都能工作,但在少数情况下,它们会失败,并出现404(未找到)错误 例如,下面是来自调用两个REST方法的测试客户端的代码: GetById("inventories"); . . . GetById("sitemapping"); private void GetById(string typeName) { // Had to add the "consta
GetByID(intsomeint)
方法。在大多数情况下,它们都能工作,但在少数情况下,它们会失败,并出现404(未找到)
错误
例如,下面是来自调用两个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));
}
}
站点映射(不工作/找不到(404):
为什么清单有效,站点映射失败
使现代化
对于基思:
URL(示例)包括:
-以及:
我可以直接在我的浏览器中添加第一条记录,它工作得很好:它在Chrome中以XML的形式返回预期的“记录”,只是根据代码显示调用失败
至于路由配置,我使用的是Castle Windsor,在RepositoriesInstaller中的代码如下:
Component.For<IInventoryRepository>().ImplementedBy<InventoryRepository>().LifestylePerWebRequest(),
. . .
Component.For<ISiteMappingRepository>().ImplementedBy<SiteMappingRepository>().LifestylePerWebRequest(),
不工作(仅限于GetByID()方法)(站点映射):
请求使用的URL是什么?你的MVC路由配置代码是什么?你能分享一下你的路由是什么样子吗?@KiranChalla:再次更新。我希望看到用于发出请求的确切URL。你可以很容易地看到。Fiddler在Chrome上不太好/容易使用,IE出于某种原因是brain de我的安装广告。确切的url是:
public class Inventory
{
[Key]
public int Id { get; set; }
[Required]
public string InventoryName { get; set; }
. . .
}
private readonly List<SiteMapping> siteMappings = new List<SiteMapping>();
. . .
public SiteMapping GetById(int ID)
{
return siteMappings.FirstOrDefault(s => s.site_no == ID);
}
public class SiteMapping
{
public int site_no { get; set; }
public string location_num { get; set; }
}
http://localhost:28642/api/sitemapping/3
http://localhost:28642/api/inventories/42
Component.For<IInventoryRepository>().ImplementedBy<InventoryRepository>().LifestylePerWebRequest(),
. . .
Component.For<ISiteMappingRepository>().ImplementedBy<SiteMappingRepository>().LifestylePerWebRequest(),
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);
}
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);
}