我创建了一个Web服务,Get工作正常,但帖子不是



我在C#中创建了一个WebService。我遇到了:

1- get工作正常,返回200:

(a(localhost -localhost

(b(PC-远程服务器

2-帖子工作正常,返回200:

(a(localhost -localhost(jquery ajax(

3-帖子返回500(内部服务器错误(:

(a(Postman(localhost -localhost(

(b(Postman(PC-远程服务器(

接口

[ServiceContract]
public interface IService1
{
    [OperationContract]
    string GetData(int value);
    [OperationContract]
    CompositeType GetDataUsingDataContract(CompositeType composite);
    [OperationContract]
    [System.ServiceModel.Web.WebInvoke(Method = "GET", RequestFormat = WebMessageFormat.Json, ResponseFormat = WebMessageFormat.Json)]
    string getable(string value);
    [OperationContract]
    [System.ServiceModel.Web.WebInvoke(Method = "POST", RequestFormat = WebMessageFormat.Json, ResponseFormat = WebMessageFormat.Json)]
    string postable(string value);
}

WebService

public class Service1 : IService1
{
    public string GetData(int value)
    {
        return string.Format("You entered: {0}", value);
    }
    public string getable(string value)
    {
        return string.Format("You entered: {0}" , value);
    }
    public string postable(string value)
    {
        return string.Format("You entered: {0}", value);
    }
    public CompositeType GetDataUsingDataContract(CompositeType composite)
    {
        if (composite == null)
        {
            throw new ArgumentNullException("composite");
        }
        if (composite.BoolValue)
        {
            composite.StringValue += "Suffix";
        }
        return composite;
    }
}

web.config(image(

在此处输入图像描述


编辑

VS输出日志

应用程序见解遥测(未配置(:{" name":" Microsoft.ApplicationInsights.dev.request"," Time":" 2018-02-15T11:26:57.6572654Z"Internal.SdkVersion":" Web:2.0.0.25000"," AI.Device.Roleinstance":" xxxxxxxxxxxxxxxxxxxx com.my"," ai.location.ip":" 127.0.0.0.1"名称":" post/service1.svc/postable","ai.operation.id":"uo2rsvl2ofq ="}," id":" uo2rsvl2ofq ="," name":":00:00.0.0279599","成功":false,"响应":" 500"," url":" http://http://localhost:60121/service1.svc/postable"," postable"," httpmethod":httpmethod":":{" developermode":" true"}}}}

在Postman

方法:帖子

url:http://localhost:60121/service1.svc/postable

标题:content-type,application/json

正文:值,任何string

返回:

{ " exceptiondetail":null, " exceptytype":null, "消息":"由于内部错误,服务器无法处理请求。有关此错误的更多信息,请在服务器上打开IncludeexeceptionDetailinFaults(来自ServiceBehaviorAttribute或来自配置行为(,以发送异常信息返回客户端,或根据Microsoft .NET Framework SDK文档打开跟踪,并检查服务器跟踪日志。 " stacktrace":null}

我找到了解决问题的解决方案。我只是停止使用WCF Web服务。

我开始使用MVC Web API,并且工作正常。它更容易使用。

您可以在此处找到该教程:https://lealen.microsoft.com/en-us/aspnet/web-api/overview/getting-started-with-with-aspnet-web-api/tutorial-your-your-first-web-api

,您可以遵循本教程:https://www.youtube.com/watch?v=0eguix3nkjg(直接请参见分钟8:39中的代码(

相关内容

  • 没有找到相关文章

最新更新