0

我想创建一个 Web 应用程序,它接受一个 POST 方法来更新数据库,使用 ReSTful 结构。

我的问题是,当我将 JSON 对象发布到 URL(使用提琴手)时,我收到错误 404。

我尝试调用的方法使用以下属性公开:

[WebInvoke(Method="POST", UriTemplate="projectors", RequestFormat=WebMessageFormat.Json,BodyStyle=WebMessageBodyStyle.Wrapped )]
[OperationContract]
void RecordBreakdown(string sn, int modelCode); 

Web 配置文件有一个绑定到暴露方法的服务,下面是一个抽象:

<system.serviceModel>
  <bindings/>
  <services>
      <service name="VIServiceToolServiceLibrary.ProjectorService"
               behaviorConfiguration="RESTBehaviour">
      <endpoint address="" 
              binding="webHttpBinding"
              contract="VIServiceToolServiceLibrary.IProjectorService"
               behaviorConfiguration="RESTEndPointbehaviour"  />
      <host>
        <baseAddresses>
          <add baseAddress="http://localhost:8080/projectorservice/" />
        </baseAddresses>
      </host>
    </service>
  </services>

<behaviors>      
  <serviceBehaviors>
    <behavior name="RESTBehaviour">
      <serviceMetadata httpGetEnabled="true"/>
      <serviceDebug includeExceptionDetailInFaults="true"/>
    </behavior>
  </serviceBehaviors>      
  <endpointBehaviors>
    <behavior name="RESTEndPointbehaviour">
      <webHttp/>
    </behavior>
  </endpointBehaviors>      
</behaviors>

如果我在 VS 或 IIS 中运行 Web 应用程序,我可以看到 svc 文件正常:

http://localhost:5970/Service.svc
http://localhost/vi/Service.svc

但是当我发布请求时,我收到了错误 404 消息

POST http://localhost/vi/projectors HTTP/1.1
User-Agent: Fiddler
Host: localhost
Content-Length: 27

{sn:"23434", modelCode:34 }

谢谢

4

1 回答 1

1

发生错误 404 有两个原因。

第一个:基地址不正确。我使用的是已被取代的早期版本的命名约定

第二:POST方法中的url不正确。我应该在 url 中包含服务文件的名称,例如 localhost/vi/Service.svc/projectors

我还发现以下文章对 解决问题很有帮助http://msdn.microsoft.com/en-us/library/dd203052.aspx

于 2013-05-07T12:22:37.830 回答