【问题标题】:Web api app got Http error 404.0 after published?Web api 应用程序发布后出现 Http 错误 404.0?
【发布时间】:2015-07-06 22:08:52
【问题描述】:

我创建了一个简单的 Asp.net web api 应用程序。它在 Visual Studio 中运行时有效。在 Visual Studio 2013 中调试时,该 url 为 http://localhost:63082/api/products/1507

我在服务器上创建了一个 IIS 网站(端口为 8081 的 mySite)并使用文件共享发布它。但是,在服务器上尝试以下 url 时总是出现 404 错误。

http://localhost:8081/api/products/1507 (它在错误页面中显示物理路径为 c:\inetpub\wwwroot\mySite\api\products\1507)。

http://localhost:8081/mySite/api/products/1507 (显示物理路径为 c:\inetpub\wwwroot\mySite\mySite\api\products\1507)。

为什么网址不起作用?

控制器方法。

    [Route("api/Products/{fileId}")]
    public IEnumerable<Product> GetProductsByFileId(int fileId)
    {
        using (var db = new ProductContext())
        {
            var query = from b in db.Products where b.fileId == fileId select b;
            if (query == null)
            {
                return null;
            }
            return query.ToList();
        }
    }

以下显示所有已发布的文件。该应用程序似乎没有二进制文件?是 Visual Studio 发布的问题吗?

目录:\\......\c$\inetpub\wwwroot\mySite 模式 LastWriteTime 长度 名称 ---- ------------- ------ ---- d---- 2015 年 7 月 6 日下午 6:21 -a--- 2015 年 7 月 6 日上午 11:23 101 Global.asax -a--- 2015 年 7 月 6 日下午 4:50 574 packages.config -a--- 2015 年 7 月 6 日下午 5:41 4611 Web.config 目录:\\......\c$\inetpub\wwwroot\mySite\bin 模式 LastWriteTime 长度 名称 ---- ------------- ------ ---- -a--- 2015 年 7 月 6 日下午 3:11 5185232 EntityFramework.dll -a--- 2015 年 7 月 6 日下午 3:11 599760 EntityFramework.SqlServer.dll -a--- 2015 年 7 月 6 日上午 11:21 502272 Newtonsoft.Json.dll -a--- 2015 年 7 月 6 日上午 11:21 185032 System.Net.Http.Formatting.dll -a--- 2015 年 7 月 6 日上午 11:21 471240 System.Web.Http.dll -a--- 2015 年 7 月 6 日上午 11:21 82120 System.Web.Http.WebHost.dll -a--- 2015 年 7 月 6 日下午 5:41 109056 webapi.dll

【问题讨论】:

  • 您需要在 IIS 端进行更多检查。操作系统版本是多少? .NET 4 是否已正确注册?很多事情需要验证。
  • 我在发布之前安装了 .net 4.5.2。操作系统版本为Windows 2008,如何查看.net 4.5.2是否注册正确?

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


【解决方案1】:

试试这个:

[Route("api/Products/{fileId}")]
public IHttpActionResult Get(int fileId)
{
    Ok(GetProductsByFileId(fileId));
}

private IEnumerable<Product>  GetProductsByFileId(int fileId)
{
    using (var db = new ProductContext())
    {
        var query = from b in db.Products where b.fileId == fileId select b;
        if (query == null)
        {
            return null;
        }
        return query.ToList();
    }
}

【讨论】:

    猜你喜欢
    • 2018-03-11
    • 2020-09-03
    • 2020-01-09
    • 2012-11-17
    • 2015-10-20
    • 1970-01-01
    • 2019-01-09
    • 2014-04-19
    相关资源
    最近更新 更多