繁体   English   中英

当localhost:端口号在iis express中的浏览器地址时,web api显示403.14错误

[英]web api shows 403.14 error when localhost:port number is in browser address in iis express

这必须是非常愚蠢的东西,但我想不出还有什么可做的。

使用Visual Studio 2013 - Update 1,我在现有解决方案中创建了一个空的web api 2项目,添加了跨源支持(cors)包并创建了一个基本的web api控制器。

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 }
        );
    }

还有Global.asax

    protected void Application_Start()
    {
        GlobalConfiguration.Configure(WebApiConfig.Register);
    }

然后我运行应用程序,IIS express正常启动,浏览器以应用程序的url启动,但似乎没有任何工作。

如果URL是“localhost:端口号”,我得到HTTP错误403.14 - 禁止Web服务器配置为不列出此目录的内容

如果我尝试“localhost:port number / api”,我会收到HTTP错误404.0 - 未找到您要查找的资源已被删除,名称已更改或暂时不可用。

我查看了几个博客,教程,示例,我还没有看到任何特殊需要做的事情。 有人可以对我可能遗失的内容有所了解吗?

Web Api没有可以通过导航到根目录查看的默认可查看页面(aspx,html等)(本例中为localhost:port )。 这是正常的行为。 要通过控制器访问Api,您需要使用MapHttpRoute()方法中指定的路径模板访问它。

因此,要在Api中访问GET方法,您将打开浏览器并将localhost:port/api/{controllername}放入url。 {controllername}将设置为控制器类的名称,而不将Controller添加到末尾。

例如:如果您的控制器看起来像这样:

public class TestController : ApiController {
    public HttpResponseMessage Get() {
          return something;
    }

    public HttpResponseMessage Get(int id) {
          return something with id;
    } 
}

然后你的第一个Get() url看起来像这样:

localhost:port/api/test

第二个Get(int id)的url如下所示:

localhost:port/api/test/5

如果您的路由配置确定无误,您可以尝试在Web.config中添加它:

<system.webServer>
 <modules runAllManagedModulesForAllRequests="true"/>
</system.webServer>

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM