【问题标题】:Web API PUT returning 404Web API PUT 返回 404
【发布时间】:2015-04-04 05:09:37
【问题描述】:

我请求使用 Angular js 放入 Web API,但我收到 404 响应。获取和发布工作很棒。可能是什么原因。我瞪大了眼睛,人们在谈论 webdev 我几乎尝试了所有东西,仍然是愚蠢的 404。

请求者和 API 都在同一个域中,http://locathost/apihttp://localhost/app

服务器端代码

  // PUT: api/Projects/5
    [ResponseType(typeof(void))]
    public async Task<IHttpActionResult> PutProject(int id, Project project)
    {
        if (!ModelState.IsValid)
        {
            return BadRequest(ModelState);
        }

        if (id != project.Id)
        {
            return BadRequest();
        }

        db.Entry(project).State = EntityState.Modified;

        try
        {
            await db.SaveChangesAsync();
        }
        catch (DbUpdateConcurrencyException)
        {
            if (!ProjectExists(id))
            {
                return NotFound();
            }
            else
            {
                throw;
            }
        }

        return StatusCode(HttpStatusCode.NoContent);
    }

AngularJs 服务

mediaApp.factory('Api', function ($resource) {
var data = $resource('http://localhost/api/projects/:id', {id: '@id'}, {
    update:{
        method:'PUT'
    }
});
return data;

});

angularjs 控制器调用它

  Api.update({ id: 9}, {id:9,projectName: 'test', number: 'test', dateTime: 'test', inspector: 'test', neighborhood: 'test', field1: 'test', field2: 'test' });

以及我得到的错误

 Remote Address:127.0.0.1:8888
Request URL:http://localhost/api/projects/9
Request Method:PUT
Status Code:404 Not Found
Request Headersview source
Accept:application/json, text/plain, */*
Accept-Encoding:gzip, deflate, sdch
Accept-Language:en-US,en;q=0.8
Content-Length:136
Content-Type:application/json;charset=UTF-8
Host:localhost
Origin:http://localhost
Proxy-Connection:keep-alive
Referer:http://localhost/App/
User-Agent:Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/40.0.2214.94 Safari/537.36
Request Payloadview source
{id: 9, projectName: "test", number: "test", dateTime: "test", inspector: "test", neighborhood: "test",…}
dateTime: "test"
field1: "test"
field2: "test"
id: 9
inspector: "test"
neighborhood: "test"
number: "test"
projectName: "test"
Response Headersview parsed
HTTP/1.1 404 Not Found
Cache-Control: private
Content-Type: text/html; charset=utf-8
Server: Microsoft-IIS/7.5
X-Powered-By: ASP.NET
Date: Sat, 04 Apr 2015 05:03:54 GMT
Content-Length: 5270

【问题讨论】:

  • 您可能需要在服务 url 中指定端口号(IIS 运行的端口)
  • get 和 post 工作正常,你的意思是 8888 的角度服务?我试过这个但没有用。默认端口是 80,它也无济于事。

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


【解决方案1】:

看看这两个帖子

如果此帖子无法访问,您需要检查 IIS 处理程序映射以确保模块允许所有动词:

  • ExtensionlessUrlHandler-ISAPI-4.0_32bit
  • ExtensionlessUrlHandler-ISAPI-4.0_64bit
  • ExtensionlessUrlHandler-Integrated-4.0

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2014-05-07
    • 1970-01-01
    • 2012-04-23
    • 2012-08-20
    • 1970-01-01
    • 2012-06-22
    • 1970-01-01
    相关资源
    最近更新 更多