我遇到与以下问题相同的问题并尝试了答案,但从未解决问题
Web api interface works locally but not on Azure
Web API interface works locally but gets 404 after deployed to Azure Website
以及更多类似的......
当我试图打电话给api时,它说404 Not Found
我的WebApi.config文件
// Web API routes
config.MapHttpAttributeRoutes();
config.Routes.MapHttpRoute(
name: "versionApi",
routeTemplate: "api/{version}/{controller}/{id}",
defaults: new { id = RouteParameter.Optional }
);
config.Routes.MapHttpRoute(
name: "DefaultApi",
routeTemplate: "api/{controller}/{id}",
defaults: new { id = RouteParameter.Optional }
);
API控制器
[Authorize]
[RequiresSSL]
[RoutePrefix("api/v2/Configuration")]
public class ConfigurationAPIv2Controller : ApiController
{
[Dependency]
public IConfigurationServicev2 configurationService { get; set; }
[Dependency]
public IAccountService accountService { get; set; }
#region testapi
[Route("getstring")]
[HttpGet]
public IHttpActionResult getstring()
{
return Ok("Success");
}
[Route("putstring")]
[HttpPut]
public IHttpActionResult putstring()
{
return Ok("Success");
}
#endregion
文件夹结构就像:
答案 0 :(得分:1)
404错误可能是由路由问题引起的。由于您正在为Web API使用route属性。请确保GlobalConfiguration.Configure(WebApiConfig.Register);高于其他代码。
protected void Application_Start()
{
GlobalConfiguration.Configure(WebApiConfig.Register);
AreaRegistration.RegisterAllAreas();
FilterConfig.RegisterGlobalFilters(GlobalFilters.Filters);
RouteConfig.RegisterRoutes(RouteTable.Routes);
BundleConfig.RegisterBundles(BundleTable.Bundles);
}
和config.MapHttpAttributeRoutes();代码高于其他路由配置。
config.MapHttpAttributeRoutes();
config.Routes.MapHttpRoute(
name: "versionApi",
routeTemplate: "api/{version}/{controller}/{id}",
defaults: new { id = RouteParameter.Optional }
);
此外,尝试删除Controller中的以下代码,以测试它是否与从属注入模块相关。
[Dependency]
public IConfigurationServicev2 configurationService { get; set; }
[Dependency]
public IAccountService accountService { get; set; }
如果它也不适合你。在WebApiConfig类中设置IncludeErrorDetailPolicy属性后,您可以从Web服务器获取详细错误消息。
config.IncludeErrorDetailPolicy = IncludeErrorDetailPolicy.Always;
答案 1 :(得分:0)
Visual Studio不会将default.html页面添加到wwwroot和IIS。
只需在项目中将 default.html 页面添加到 wwwroot ,然后重新部署到Azure。