WebApi force action to return xml - c#

I have this action:
public IHttpActionResult SearchFor(int aboItemType, DTO.FilterColumns filter)
{
//Do stuff...
return Ok<DataSet>(ds);
}
My client does:
client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/xml"));
var response = client.PostAsJsonAsync(myurl).Result;
if (response.IsSuccessStatusCode)
{
var results = HttpUtility.HtmlDecode(response.Content.ReadAsStringAsync().Result);
}
The above scenario works perfectly. However, if I comment the Accept line, the action returns the dataset in json format.
I would like to force this one particular action to always send the result in xml. Is this possible? Maybe with an attribute?

I used Сonfiguration.Formatters.XmlFormatter
public IHttpActionResult Get()
{
...
return Content(HttpStatusCode.OK, Model, Configuration.Formatters.XmlFormatter);
}

Also you can do this (in the case you have to pass some http header values):
public IHttpActionResult Get()
{
var result = Request.CreateResponse(HttpStatusCode.OK,
model,
Configuration.Formatters.XmlFormatter);
result.Headers.Add("Access-Control-Allow-Origin", "*");
return ResponseMessage(result);
}

Related

Web API 2 return simple string without quotation mark when return type is IHttpActionResult

My question is referring to this question.
The answer shows only solutions where you would have to change the type of the method to HttpResponseMessage or string.
This is my method:
public IHttpActionResult Get()
{
return Ok("I am send by HTTP resonse");
}
It returns:
"I am send by HTTP resonse"
I expect:
I am send by HTTP response
Is there a way to return a simple string without quotation mark where the return type of the method is IHttpActionResult?
You can use ApiController.ResponseMessage(HttpResponseMessage) Method
Creates a ResponseMessageResult with the specified response.
ResponseMessageResult is derived from IHttpActionResult
public IHttpActionResult Get() {
var message = "I am send by HTTP response";
var httpResponseMessage = new HttpResponseMessage(HttpStatusCode.OK) {
Content = new StringContent(message, System.Text.Encoding.UTF8, "text/plain")
};
return ResponseMessage(httpResponseMessage);
}
Change it to:
public IHttpActionResult Get()
{
return Content("I am send by HTTP resonse");
}
The IHttpActionResult is WebApi2 way, you can also use
public HttpResponseMessage Get()
{
return Request.CreateResponse(HttpStatusCode.OK, "I am send by HTTP response");
}
Also if you expect without double quotes please refer to the link you specified in your question.

How to retrieve HTTP Code and Content from IActionResult?

I've 7 actions in my controllers. I've refactored them an was able to isolate the common portion.
public IActionResult Get()
{
var response = CommonCode();
}
public IActionResult Get(guid id)
{
var response = CommonCode();
}
public IActionResult Post(ViewModel vm)
{
var response = CommonCode();
}
This is where I refactored the common code.
provate IActionResult CommonCode()
{
if(userHasNoPermission())
return Forbid();
if(IdProvidedDoesntExist())
return BadRequest();
//...
}
When I look inside the response, I see only one method: ExecuteResultAsync().
Is there a way to retrieve the HTTP code that I sent inside the helper method?
I'd like for instance to stop the processing if it's 500, retrieve the message to add to the ModelState* if it's 400, but proceed if it's OK.
There are a few "filthy" ways you can do it without a case statement. The status code is actually in the result but IActionResult and ActionResult hate being cast to a common type.
This example takes a IActionResult from any common result and rips the status code out using reflection. If you don't mind doing it that way it saves requiring the case statement to pattern match. The same can be done for the content.
public static HttpStatusCode GetHttpStatusCode(IActionResult functionResult)
{
try
{
return (HttpStatusCode)functionResult
.GetType()
.GetProperty("StatusCode")
.GetValue(functionResult, null);
}
catch
{
return HttpStatusCode.InternalServerError;
}
}
The return of CommonCode is simply some type of IActionResult. The "result" is not actually a "response". That comes later when the action is fully returned back into the request pipeline (which has not occurred yet when you're calling the method directly in code). As result, concepts like HTTP status code aren't even relevant yet. If you return something like StatusCodeResult, that's technically only a suggested status code. If there's an exception later in the request pipeline or some piece of middleware explicitly changes the status code for some reason, it will be different.
Long and short, you're trying to conflate two unrelated things together. I think you simply want to know what happened in CommonCode, and think the HTTP status is the best way to determine that. In actuality, you'd be better served by returning a tuple or doing something like pattern matching:
With a tuple, you can essentially return more than one thing from your CommonCode method. For example, you could do something like:
private (int, IActionResult) CommonCode()
{
if(userHasNoPermission())
return (403, Forbid());
if(IdProvidedDoesntExist())
return (400, BadRequest());
//...
}
Then:
public IActionResult Get()
{
(var status, var response) = CommonCode();
// branch on `status`
}
Or, with pattern matching:
public IActionResult Get()
{
var response = CommonCode();
switch (response)
{
case ForbidResult forbid:
// do something for forbidden
break;
case BadRequestResult badRequest:
// do something for bad request
break;
}
}
I had a similar problem returning Ok() and NotFound(). I
was able to get the status code using the IStatusCodeActionResult interface.
((IStatusCodeActionResult)response).StatusCode;
You could return the statuscode itself from the private method. for example
private IActionResult CommonCode() {
if(userHasNoPermission())
return StatusCode(401);
if(IdProvidedDoesntExist())
return StatusCode(400);
//... }
then in your method just check for the status code
public IActionResult Post(ViewModel vm)
{
var response = CommonCode();
if (this.HttpContext.Response.StatusCode == 418)
Console.WriteLine("I'm a teapot")
else {
return response;
}
}

How to accept a query and return an async task?

I am getting the following exception:
Cannot create an EDM model as the action 'Get' on controller 'Accounts' has a return type 'System.Web.Http.IHttpActionResult' that does not implement IEnumerable<T>.
When attempting to query my endpoint:
http://localhost:8267/api/accounts
The AccountsController that is doing the work:
public async Task<IHttpActionResult> Get(ODataQueryOptions options)
{
var query = options.Request.RequestUri.PathAndQuery;
var client = new HttpClient();
var crmEndPoint = #"HTTPS://MYCRMORG.COM/API/DATA/V8.1/";
HttpResponseMessage response = await client.GetAsync(crmEndPoint+query);
object result;
if (response.IsSuccessStatusCode)
{
result = await response.Content.ReadAsAsync<object>();
return Ok(result);
}
return NotFound();
}
What am I doing wrong? How do I simply add the PathAndQuery to my crmEndPoint and return the result?
The OData framework provides extra response formatting/querying rules on top of plain Web API.
Using ODataQueryOptions parameter requires that the action method returns either IQueryable<T> or IEnumerable<T>.
ODataQueryOptions just helps to parse the incoming OData request url making parameters such as $filter and $sort accessible through properties.
Your code doesn't need this service because all it does is just redirect the request to the crmEndPoint. So, instead of using options.Request you can access the request object through the Request property of the controller and drop the parameter altogether.
Here's the code:
public async Task<IHttpActionResult> Get()
{
var query = Request.RequestUri.PathAndQuery;
var client = new HttpClient();
var crmEndPoint = #"HTTPS://MYCRMORG.COM/API/DATA/V8.1/";
HttpResponseMessage response = await client.GetAsync(crmEndPoint + query);
object result;
if (response.IsSuccessStatusCode)
{
result = await response.Content.ReadAsAsync<object>();
return Ok(result);
}
return NotFound();
}

IHttpActionResult with JSON string

I have a method that originally returned an HttpResponseMessage and I'd like to convert this to return IHttpActionResult.
My problem is the current code is using JSON.Net to serialize a complex generic tree structure, which it does well using a custom JsonConverter I wrote (the code is working fine).
Here's what it returns:
string json = NodeToJson(personNode);
HttpResponseMessage response = Request.CreateResponse(HttpStatusCode.OK);
response.Content = new StringContent(json, Encoding.UTF8, "application/json");
return response;
The NodeToJson method is where the custom converter comes into play ...
private static string NodeToJson(Node<Person> personNode) {
var settings = new JsonSerializerSettings {
Converters = new List<JsonConverter> { new OrgChartConverter() },
Formatting = Formatting.Indented
};
return JsonConvert.SerializeObject(personNode, settings);
}
Note this returns a string, formatted as JSON.
If I switch this to IHttpActionResult, it seems to fail regardless of what I try. I can just leave it (it works) but I am supposed to be using best practices for this and IHttpActionResult seems to be what I should be using.
I have tried to return Json(json); but this results in invalid, unparsable JSON, presumably because it's trying to do a double conversion?
return Ok(json); results in the JSON string being wrapped in XML.
What is the right way to do this?
EDIT:
I have successfully converted every method in this project to use IHttpActionResult now except this particular method.
It's a serialization of a generic tree to JSON. Regardless of what approach I try, I get back invalid JSON. The HttpResponseMsessage approach works fine, but I can not get valid JSON back with IHttpActionResult.
You can create your own IHttpActionResult class instance to return the JSON and a method in your controller or base controller class to utilize it.
Create the IHttpActionResult instance that sets the content and status code:
public class JsonTextActionResult : IHttpActionResult
{
public HttpRequestMessage Request { get; }
public string JsonText { get; }
public JsonTextActionResult(HttpRequestMessage request, string jsonText)
{
Request = request;
JsonText = jsonText;
}
public Task<HttpResponseMessage> ExecuteAsync(CancellationToken cancellationToken)
{
return Task.FromResult(Execute());
}
public HttpResponseMessage Execute()
{
var response = this.Request.CreateResponse(HttpStatusCode.OK);
response.Content = new StringContent(JsonText, Encoding.UTF8, "application/json");
return response;
}
}
Add a method to your controller to create the result. Here is a Web API example:
public class MyApiController : ApiController
{
protected internal virtual JsonTextActionResult JsonText(string jsonText)
{
return new JsonTextActionResult(Request, jsonText);
}
[HttpGet]
public IHttpActionResult GetJson()
{
string json = GetSomeJsonText();
return JsonText(json);
}
}
Another recommendation is as below;
var json = JToken.FromObject(yourObject);
return Ok(json);
I've got the same problem and this piece of code worked for me (Using Newtonsoft.Json nuget package to deserialize the json):
var unserializedContent = JsonConvert.DeserializeObject(json);
return Json(unserializedContent);
It seems we must have an object in order to Json() work as it should.
Some of the solutions here are converting string to JSON, that's not necessary.
You are just using computer resources for nothing.
// Instead of
// return Ok(jsonstring);
// do:
HttpResponseMessage response = this.Request.CreateResponse(HttpStatusCode.OK);
response.Content = new StringContent(jsonstring, Encoding.UTF8, "application/json");
Request.RegisterForDispose(response); //To avoid the Pragma CA2000 warning
return ResponseMessage(response);
Another solution At client side
You can make a small change to be prepared to receive a string and convert it if necessary. The code bellow is Javascript
var data;
if (typeof weapiresponse == "string")
data = JSON.parse(weapiresponse);
else
data = weapiresponse;
If you have no intention of using XML as a return type, you can also remove the XmlFormatter in your WebApiConfig:
config.Formatters.Remove(config.Formatters.XmlFormatter);
The correct way is to return:
Ok(json);
It's converting the result to XML because that's the default accepted return type. Try adding:
Accept: application/json into your API request headers, I think that should resolve the issue.
I had the same problem with web-service returning JSON string in a XML-tag. I tried all the simple solutions Like :
return Json(text) , json deserialize and adding config.Formatter for json, but that did't help. I got double cotes around the json object or it was malformed.
Only the solution written by TGRA worked for me.
create your own IHttpActionResult class instance to return the JSON
For me, the only way to return an IHttpActionResult with the string content as Json in the following.
[HttpGet]
public IHttpActionResult ReturnStringAsJson()
{
return this.ResponseMessage(new HttpResponseMessage
{
Content = new StringContent("[json string]"),
Encoding.UTF8,
"application/json"),
});
}

Web Api 2 bad request

Im a beginner with Web api and Im trying to setup a simple owin selfhosted service that Im trying out.
I've been searching stackoverflow and other places for a while now, but I cant seem to find anything obviously wrong.
The problem I have is that I get a bad request response everytime I try to call my service.
The controller looks like this:
[RoutePrefix("api/ndtdocument")]
public class NDTDocumentsController : ApiController, INDTDocumentsController
{
[HttpGet]
public IHttpActionResult Get()
{
var document = Program.NDTServerSession.GetNextNDTDocument(DateTime.Today);
if (document == null)
return null;
return Ok(document);
}
[Route("")]
public IHttpActionResult Post([FromBody] NDTDocument ndtDocument)
{
try
{
Program.NDTServerSession.AddNDTDocument(ndtDocument);
return Ok();
}
catch(Exception ex)
{
return BadRequest(ex.Message);
}
}
}
And the client looks like this:
static void Main(string[] args)
{
AddNDTDocument(#"C:\Testing.txt");
}
private static void AddNDTDocument(string centralserverPath)
{
var client = GetServerClient();
NDTDocument document = new NDTDocument();
var response = client.PostAsJsonAsync("ndtdocument", document).Result;
}
static HttpClient GetServerClient()
{
var client = new HttpClient();
client.BaseAddress = new Uri("http://localhost:9000/api/");
client.DefaultRequestHeaders.Accept.Clear();
client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));
return client;
}
I can see when I debug it that the request uri is infact http://localhost:9000/api/ndtdocument
But the response is allways bad request and I have a breakpoint in the controller and it is never invoked.
Everytime I try to do something with web apis I Always run into some weird (but simple problem).
Any thoughts?
Thanks!
Web API will decide your route based on your method names. Since you have added [RoutePrefix("api/ndtdocument")] on class level this will be the route to your controller. When web api looks for an action it will match on method names, so in your case your actual route would be http://localhost:9000/api/ndtdocument/post.
When trying to decide what http method that a specific action requires web api will check your method names and methods starting with post will be http post, get will be http get etc.
So lets say we would instead call our method PostData, for starters we could remove the [HttpPost] attribute. Our route would now be http://localhost:9000/api/ndtdocument/postdata. Let's now say that we want our path to be just /data. We would then first rename our method to Data, but now web api does not know what http method we want to invoke this method with, thats why we add the [HttpPost] attribute.
Edit after reading your comment
[Route("{id:int}")]
public IHttpActionResult Get(int id)
[Route("")]
public IHttpActionResult Post([FromBody] NDTDocument ndtDocument)
Okey, after nearly going seriously insane. I found the problem.
I forgot to reference webapi.webhost and then system.web.
After this Everything worked like a charm.
You must use route tags and call this way http://localhost:9000/api/get or http://localhost:9000/api/post
[RoutePrefix("api/ndtdocument")]
public class NDTDocumentsController : ApiController, INDTDocumentsController
{
[HttpGet]
[Route("get")]
public IHttpActionResult Get()
{
var document = Program.NDTServerSession.GetNextNDTDocument(DateTime.Today);
if (document == null)
return null;
return Ok(document);
}
[HttpPost]
[Route("post")]
public IHttpActionResult Post([FromBody] NDTDocument ndtDocument)
{
try
{
Program.NDTServerSession.AddNDTDocument(ndtDocument);
return Ok();
}
catch(Exception ex)
{
return BadRequest(ex.Message);
}
}
}
for more infromation pls check this link

Categories