【问题标题】:Posting an XML to WebAPI and return a result将 XML 发布到 WebAPI 并返回结果
【发布时间】:2014-10-31 09:57:07
【问题描述】:

所以一段时间以来,我一直在努力完成这项工作。 让我解释一下要求。 我必须编写一个接受 XML、查找并返回响应的 WebAPI。

我是新手,所以寻求帮助。建议是创建一个表示传入 XML 的自定义对象 并将该对象用作 WebAPI 公开的方法的参数。 结果将是一个没有问题的类。

这是完成的步骤。

创建了一个空的 WebAPI 项目。 添加了一个表示传入 XML 的类。

传入的 XML:

<InComingStudent>
<StudentID>10</StudentID>
<Batch>56</Batch>
</InComingStudent>

班级:

public class InComingStudent
{
    public string StudentID { get; set; }
    public string Batch { get; set; }
}

返回此类型的对象:

public class StudentResult
{
    public string StudentID { get; set; }
    public string Batch { get; set; }
    public string Score { get; set; }
}

使用此方法添加了一个学生控制器:

public class StudentsController : ApiController
{
    [HttpPost]
    public StudentResult StudentStatus(InComingStudent inComingStudent)
    {

        ProcessStudent po = new ProcessStudent();
        StudentResult studentResult = po.ProcessStudent();
        return StudentResult;
    }
}

运行服务。它在一个新的浏览器中打开,显示 404。没关系,因为我没有起始页。

编写了一个控制台应用程序进行测试:

private static async void PostToStudentService()
{
    using (var client = new HttpClient())
    {
        var studentToPost = new InComingStudent() { StudentID = "847", Batch="56"};
        client.BaseAddress = new Uri("http://localhost:53247/");
        client.DefaultRequestHeaders.Accept.Clear();
        client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/xml"));

        HttpResponseMessage response = await client.PostAsXmlAsync("api/Students/StudentStatus", studentToPost);

        if (response.IsSuccessStatusCode)
        {
            // Print the response
        }
    }
}

MediaTypeWithQualityHeaderValue 设置为 application/xml。 到目前为止,一切都很好。 当服务正在运行并且我运行控制台应用程序时,响应是 404“未找到”。

我错过了什么?

非常感谢任何帮助。

问候。

【问题讨论】:

    标签: c# xml asp.net-web-api


    【解决方案1】:

    你试过[FromBody]属性吗?

    public class StudentsController : ApiController
    {
        [HttpPost]
        public StudentResult StudentStatus([FromBody]InComingStudent inComingStudent)
        {
            ProcessStudent po = new ProcessStudent();
            StudentResult studentResult = po.ProcessStudent();
            return StudentResult;
        }
    }
    

    作为建议,我会使用属性路由。

    public class StudentsController : ApiController
    {
        [HttpPost, Route("api/stutents/studentsstatus")]
        public StudentResult StudentStatus([FromBody]InComingStudent inComingStudent)
        {
            // ...
       }
    }
    

    【讨论】:

    • 首先感谢您的时间和帮助。服务到达断点,但 InComingStudent 为 null。任何线索!!!
    • Nothing 只是添加了 [FromBody] 并使用 bp 运行服务。运行传入 Student 对象的控制台应用程序。
    • 您能否使用 fiddler(telerik 的免费工具)来分析究竟是什么通过了网络?使用 fiddler,您可以查看 XML 格式是否正确并包含您传入的所有信息。
    • 在 Composer 中,将类型设置为 POST,将服务的 URL 和 XML 粘贴到请求正文区域。点击执行。这一次甚至没有打到服务中的bp。提琴手说 415。
    • 如果你运行你的控制台应用程序,你能在 fiddler 中看到这个请求吗?正文中的 XML 是否有效?
    猜你喜欢
    • 2011-12-02
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多