【问题标题】:WebAPI displays 404 error when deploying on Server using IIS 7 but works fine as selfhost使用 IIS 7 在服务器上部署时 WebAPI 显示 404 错误,但作为 selfhost 工作正常
【发布时间】:2015-09-24 12:27:12
【问题描述】:

任何帮助将不胜感激。我已经研究这个问题几天了。这就是正在发生的事情。

我在 vs 2012 中使用 mvc4 下的模板创建了我的第一个 WebApi 服务。我保留了大部分默认设置。我确实在 WebApiConfig 中更改了路由器。

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web.Http;

namespace GetBalanceSV
{
    public static class WebApiConfig
    {
        public static void Register(HttpConfiguration config)
        {
            config.Routes.MapHttpRoute(
                name: "DefaultApi",
                routeTemplate: "SubscriberActivity/{controller}/{id}",
                defaults: new { id = RouteParameter.Optional }
            );
        }
    }
}

我可以在我的机器上以调试模式正常运行。我通过 VS2012 在我的机器上发布并使用 IIS 作为自主机运行它,并且可以调用 get 方法并毫无问题地得到响应。使用

"http://localhost/SubscriberActivity/Values/Testingingmystring"

我也可以将它部署在我们的 Web 开发服务器上,使用 IIS 作为自托管中的一个新网站,这样就可以在服务器上正常运行。但是,当我使用相同的应用程序池放置在现有网站下时,它会失败。 404 错误。

我通过 fiddler2 运行了这个,这就是我得到的响应。

*HTTP/1.1 404 Not Found
Cache-Control: private
Content-Type: text/html; charset=utf-8
Server: Microsoft-IIS/7.5
X-AspNet-Version: 4.0.30319
X-Powered-By: ASP.NET
Date: Mon, 07 Oct 2013 17:04:59 GMT
Content-Length: 1937
<!DOCTYPE html>
<html>
    <head>
        <title>The resource cannot be found.</title>
        <meta name="viewport" content="width=device-width" />
        <style>
         body {font-family:"Verdana";font-weight:normal;font-size: .7em;color:black;} 
         p {font-family:"Verdana";font-weight:normal;color:black;margin-top: -5px}
         b {font-family:"Verdana";font-weight:bold;color:black;margin-top: -5px}
         H1 { font-family:"Verdana";font-weight:normal;font-size:18pt;color:red }
         H2 { font-family:"Verdana";font-weight:normal;font-size:14pt;color:maroon }
         pre {font-family:"Consolas","Lucida Console",Monospace;font-size:11pt;margin:0;padding:0.5em;line-height:14pt}
         .marker {font-weight: bold; color: black;text-decoration: none;}
         .version {color: gray;}
         .error {margin-bottom: 10px;}
         .expandable { text-decoration:underline; font-weight:bold; color:navy; cursor:hand; }
         @media screen and (max-width: 639px) {
          pre { width: 440px; overflow: auto; white-space: pre-wrap; word-wrap: break-word; }
         }
         @media screen and (max-width: 479px) {
          pre { width: 280px; }
         }
        </style>
    </head>
    <body bgcolor="white">
            <span><H1>Server Error in '/SubscriberActivity' Application.<hr width=100% size=1 color=silver></H1>
            <h2> <i>The resource cannot be found.</i> </h2></span>
            <font face="Arial, Helvetica, Geneva, SunSans-Regular, sans-serif ">
            <b> Description: </b>HTTP 404. The resource you are looking for (or one of its dependencies) could have been removed, had its name changed, or is temporarily unavailable. &nbsp;Please review the following URL and make sure that it is spelled correctly.
            <br><br>
            <b> Requested URL: </b>/SubscriberActivity/values/testeing<br><br>
    </body>
</html>*

有人知道为什么会发生这种情况吗?

【问题讨论】:

  • 你有没有想过这个问题?我现在有同样的问题......也许这是一个天蓝色的问题,因为这似乎是趋势?

标签: asp.net-mvc-4 asp.net-web-api


【解决方案1】:

检查现有应用程序池是处于经典模式还是集成模式。如果是经典模式,则将其更改为集成模式

【讨论】:

    【解决方案2】:

    我不确定您所说的“但是当我使用相同的应用程序池放置在现有网站下时它失败了”是什么意思。 ...但无论如何,我相信您的问题在于:

    • 也许 iis 需要更新以支持无扩展 url(除非它已经很好地托管在相同的 iis 上,这意味着它已经支持它)。检查this

    • 确保您的 http 处理程序正确(查看 Visual Studio 中 web.config 中的部分,并确保部署的版本具有相同的 http 处理程序)。恐怕你的部署方式不对。

    您也可以查看link

    【讨论】:

    • 感谢您的回复,A Khudairy。我的报价“但是,当我使用相同的应用程序池放置在现有网站下时,它失败了。”我的意思是在 IIS 通常附带的现有“默认网站”下添加我的 Web 服务。 Web 服务不会响应。如果我右键单击“站点”并单击添加新网站,它将起作用。也许它需要在自己的网站上运行?
    • 我右键单击默认网站节点并选择添加应用程序。不会工作
    • 尝试从 Visual Studio 发布网站。
    猜你喜欢
    • 1970-01-01
    • 2023-03-08
    • 2013-02-14
    • 2011-07-21
    • 2018-10-22
    • 2013-05-21
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多