好吧,我很难找到问题所在,因为它在本地工作,但发布后结果很简单:
错误代码:403 禁止。服务器拒绝指定的统一资源定位器 (URL)。请联系服务器管理员。 (12202)
代码:
[RoutePrefix("api/v1/project")]
public class ProjectController : BaseApiController
{
[HttpGet]
public HttpResponseMessage GetProjects()
{
HttpResponseMessage resp = new HttpResponseMessage(HttpStatusCode.OK);
if(User.Identity.IsAuthenticated)
{
var model = new ModelFactory().CreateProjects();
resp = Request.CreateResponse(HttpStatusCode.OK, model);
}
return resp;
}
}
<小时/>
public static class WebApiConfig
{
public static void Register(HttpConfiguration config)
{
// all actions under /project routes require authentication
config.Routes.MapHttpRoute(
name: "ProjectApi",
routeTemplate: "api/v1/{controller}/{action}/{apikey}",
defaults: new { apikey = RouteParameter.Optional },
constraints: new { controller = "project" },
handler: new BasicAuthHandler(config));
// all routes requires an api key
config.MessageHandlers.Add(new ApiKeyHandler());
config.MapHttpAttributeRoutes();
}
}
<小时/>
我已经尝试了网上的几种“解决方案”,但似乎都没有解决这个问题。我添加了:
// Stop IIS/Asp.Net breaking our routes
RouteTable.Routes.RouteExistingFiles = true;
来自:http://www.grumpydev.com/2013/09/17/403-14-error-when-trying-to-access-a-webapi-route/
并且还确保:
<modules runAllManagedModulesForAllRequests="true">
<小时/>
有了上面的代码,使用以下链接可以成功连接,它会检查(以正确的顺序)APIkey (ApiKeyHandler),检查用户是否需要登录(BasicAuthHandler),然后转到 Controller 中的方法({ Controller }/{操作})。
// THIS WORKS!
http://localhost:51077/api/v1/project/getprojects?apikey=123456
然后我们发布并尝试同样的事情
// This is haunted with number 403
http://website.com/api/v1/project/getprojects?apikey=123456
给出错误代码:403 Forbidden。
我一无所知。我什至尝试将“NETWORK SERVICE”的整个发布文件夹的安全设置更改为完全访问权限。没有任何更改。
如果您需要更多信息,请告诉我。
最佳答案
给 Web 服务器机器打电话,他们有防火墙通过身份验证阻止传入的 WebAPI 调用。现在它可以正常工作了:)
关于asp.net-web-api - 网络API : 403 Forbidden after publish website,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30454519/