[英]web api shows 403.14 error when localhost:port number is in browser address in iis express
This has to be something really dumb but I can't think what else to do. 这必须是非常愚蠢的东西,但我想不出还有什么可做的。
Using Visual Studio 2013 - Update 1, I created an empty web api 2 project in an existing solution, added the cross origin support (cors) package and created a basic web api controller. 使用Visual Studio 2013 - Update 1,我在现有解决方案中创建了一个空的web api 2项目,添加了跨源支持(cors)包并创建了一个基本的web api控制器。
The WebApiConfig class seems to be fine: WebApiConfig类看起来很好:
public static void Register(HttpConfiguration config)
{
// Web API configuration and services
var cors = new EnableCorsAttribute("*","*","*");
config.EnableCors(cors);
// Web API routes
config.MapHttpAttributeRoutes();
config.Routes.MapHttpRoute(
name: "DefaultApi",
routeTemplate: "api/{controller}/{id}",
defaults: new { id = RouteParameter.Optional }
);
}
And also the Global.asax 还有Global.asax
protected void Application_Start()
{
GlobalConfiguration.Configure(WebApiConfig.Register);
}
I then run the application, IIS express starts normally and the browser starts with the application's url but nothing seems to work. 然后我运行应用程序,IIS express正常启动,浏览器以应用程序的url启动,但似乎没有任何工作。
If the URL is "localhost:port number" I get HTTP Error 403.14 - Forbidden The Web server is configured to not list the contents of this directory . 如果URL是“localhost:端口号”,我得到HTTP错误403.14 - 禁止Web服务器配置为不列出此目录的内容 。
If I try "localhost:port number/api" I get HTTP Error 404.0 - Not Found The resource you are looking for has been removed, had its name changed, or is temporarily unavailable. 如果我尝试“localhost:port number / api”,我会收到HTTP错误404.0 - 未找到您要查找的资源已被删除,名称已更改或暂时不可用。
I have looked at several blogs, tutorials, examples and I haven't seen anywhere that anything special needs to be done. 我查看了几个博客,教程,示例,我还没有看到任何特殊需要做的事情。 Could someone please shed some light in what I might be missing?
有人可以对我可能遗失的内容有所了解吗?
Web Api doesn't have a default viewable page (aspx, html, etc) that can be viewed by navigating to the root ( localhost:port
in this case). Web Api没有可以通过导航到根目录查看的默认可查看页面(aspx,html等)(本例中为
localhost:port
)。 So that is the normal behavior. 这是正常的行为。 In order to access your Api through the controller you need to access it using the route template specified in your
MapHttpRoute()
method. 要通过控制器访问Api,您需要使用
MapHttpRoute()
方法中指定的路径模板访问它。
So to access the GET
method in your Api you would open a browser and place localhost:port/api/{controllername}
into the url. 因此,要在Api中访问
GET
方法,您将打开浏览器并将localhost:port/api/{controllername}
放入url。 {controllername}
would be set to the name of your controller class without Controller added to the end. {controllername}
将设置为控制器类的名称,而不将Controller添加到末尾。
ex: If your controller looked like this: 例如:如果您的控制器看起来像这样:
public class TestController : ApiController {
public HttpResponseMessage Get() {
return something;
}
public HttpResponseMessage Get(int id) {
return something with id;
}
}
Then your url for the first Get()
would look like this: 然后你的第一个
Get()
url看起来像这样:
localhost:port/api/test
And the url for the second Get(int id)
would look like this: 第二个
Get(int id)
的url如下所示:
localhost:port/api/test/5
If yours route config is OK for sure, you can try add this in Web.config: 如果您的路由配置确定无误,您可以尝试在Web.config中添加它:
<system.webServer>
<modules runAllManagedModulesForAllRequests="true"/>
</system.webServer>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.