我可以在ASP.NET Web API(MVC 4)中接收XML请求吗? [英] Can I receive XML request in ASP.NET web API (MVC 4)

查看:475
本文介绍了我可以在ASP.NET Web API(MVC 4)中接收XML请求吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用带有基本身份验证的asp.net创建了一个WEB API。

这是我的获取方法链接:

I created a WEB API using asp.net with basic authentication.
Here is my get Method link :

localhost:XXX/api/Products/X1



但客户需要XML格式的请求和响应,其中包含产品ID和身份验证凭证。



示例:


But the client need the request and response in XML format with Products id and authentications credential included in it .

Example :

<Request xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://www.ssss/">
  <wsVersion>1</wsVersion>
  <id>user1</id>
  <password>pass1</password>
  <productID>X1</productID>
  <ptype>test</ptype>
</Request>





这是GET的产品控制器方法





this is the product controller method for GET

public HttpResponseMessage Get(string id)
     {
         var product = _productService.Get(id);
         if (product != null)
             return Request.CreateResponse(HttpStatusCode.OK, product);
         return Request.CreateErrorResponse(HttpStatusCode.NotFound, "data not found for provided id.");
     }







我可以将请求格式更改为我的XML格式吗? WEB API?或者我需要为此创建一个WCF服务?



我尝试过:



我是否需要将WEBapi更改为WCF服务?




Can I change the request format as XML inside my WEB API ? or I need to create a WCF service for this ?

What I have tried:

Do I need change the WEBapi to WCF service ?

推荐答案

您应该只返回您的对象,而不应该关注其XML或JSON。客户有责任从web api请求JSON或XML。



为了测试这个,我只需使用Fiddlder并通过指定请求向web api发出请求标题(json或xml)。



You should simply return your object, and shouldn't be concerned about whether its XML or JSON. It is the client's responsibility to request JSON or XML from the web api.

To test this I would simply use Fiddlder and make a request to the web api by specifying request header (either json or xml).

Accept: application/xml


这篇关于我可以在ASP.NET Web API(MVC 4)中接收XML请求吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆