Web APIを含むASP.NET Webforms Webサイトがあります。このサイトは、WebサーバーとしてIIS Expressを使用してWindows 8上でVisual Studio 2013および.NET 4.5で開発およびテストされています。WebフォームでWeb APIを使用すると404エラーが返される
私は次のように定義されたルートディレクトリ内のWeb APIのコントローラを追加しました:
[RoutePrefix("api")]
public class ProductsController : ApiController
{
Product[] products = new Product[]
{
new Product { Id = 1, Name = "Tomato Soup", Category = "Groceries", Price = 1 },
new Product { Id = 2, Name = "Yo-yo", Category = "Toys", Price = 3.75M },
new Product { Id = 3, Name = "Hammer", Category = "Hardware", Price = 16.99M }
};
[Route("ProductsController")]
[HttpGet]
public IEnumerable<Product> GetAllProducts()
{
return products;
}
public Product GetProductById(int id)
{
var product = products.FirstOrDefault((p) => p.Id == id);
if (product == null)
{
throw new HttpResponseException(HttpStatusCode.NotFound);
}
return product;
}
public IEnumerable<Product> GetProductsByCategory(string category)
{
return products.Where(
(p) => string.Equals(p.Category, category,
StringComparison.OrdinalIgnoreCase));
}
}
Global.asaxのは、次のようになります。私はこれらの2行でが含まれている
public class Global : HttpApplication
{
void Application_Start(object sender, EventArgs e)
{
// Code that runs on application startup
AreaRegistration.RegisterAllAreas();
RouteConfig.RegisterRoutes(RouteTable.Routes);
RouteTable.Routes.MapHttpRoute(
name: "DefaultApi",
routeTemplate: "api/{controller}/{id}",
defaults: new { id = System.Web.Http.RouteParameter.Optional });
}
}
私のweb.configファイル
<validation validateIntegratedModeConfiguration="false"/>
<modules runAllManagedModulesForAllRequests="true"/>
次のURLでgetリクエストを行うと、http://localhost:5958/api/products、HTTPエラー404.0が表示されます。私は別の解決策を試みたが、何も働かない。私が紛失しているものがありますか?この問題を解決するにはどうすればよいですか?
ありがとうございます。
私はそれをすべてやっていますが、それでも同じ問題です。 web.configで何かを変更する必要がありますか? –