I am trying to format an ASP.NET Core OData response to Atom+xml to retain backward compatibility.
Here is the code I tried but I still have no luck. Is there a way I can format response in atom+xml ?
builder.services.AddMvc().AddMvcOptions (o =>{
o.OutputFormatters.Add(new XmlDataContractSerializerOutputFormatter());
});
The ASP.NET Core code is clearing the headers in the call and adding the new header value and asp.net code is format is getting changed to atom xml.
this.Request.Headers.Clear()
this.Request.Headers.Accept.Add(new MediaTypeWithQualityHeaderValue("application/atom+xml")
Related
I have some logging middleware to get request body in aspnet API that was working fine for dotnet5. But since I upgraded to dotnet6, I noticed that HttpContext.Request.Body has zero length.
My code so far:
context.Request.EnableBuffering();
await using var tmpStream = RecyclableMemoryStreamManager.GetStream();
await context.Request.Body.CopyToAsync(tmpStream); // context.Request.Body.Length is 0
Is there some way to get request body in dotnet6?
I read there's a Microsoft middleware to log request, but I would prefer to use our implementation, which have custom features.
I'm creating a web application using VueJS, C# and Entity Framework, but there is a problem. I don't know how to return a JSON using Axios to backend. In this project I'm using dynamic forms based on JSON schema and what I'm trying to do is when the user has completed the required fields, once he clicks on the 'send' button, that generated JSON should be redirected to my database where it should parse into a table with it's values.
Here is the function that "should" return the completed JSON but no success
returnJSON(){
axios
.post('http://localhost/jsonSchema/email.json')
.then(response => (this.info = response))
}
I'm not allowed to share more code, but i can provide a link from where I took the JSON schema:
https://github.com/koumoul-dev/vuetify-jsonschema-form. I've tried json server as well, but no success, because once I change those fields into objects, the schema is not working anymore.
I have two questions:
How can I return a link that contains the resulted JSON?
Should I look at this problem from another point of view? Is it better to parse JSON first in C# then send that data to database instead of sending just an endpoint to be parsed directly into a stored procedure?
Basically, the json schema does not affect how you send data back to the backend server, it just enforces that the data (form data in this case) follows a certain structure. Therefor:
Simply look at your form v-model:
<v-jsf v-model="model" :schema="schema" :options="options" />
...which is model in this case. This variable holds your data. Now send it to the server in your submit method:
async returnJSON(){
this.info = await this.$axios.post('<YOUR SERVER URL>', this.model).then(response => response.data)
}
Note that I changed your returnJSON method to use async/await syntax, which is far more understandable here and also made it use response.data aswell as called axios in the way I suspect you need to call it (depending on your setup).
With Unity 2018.4.11f1 we want to use a Google REST API from a Unity application and found that a working example is using obsolete WWW.
Updating it with UnityWebRequest.Post() is always returning error 400:Invalid JSON payload received. Expected a value.
The Unity documentation says the default Content-Type header is set to application/x-www-form-urlencoded and the payload data seems to be converted into an appropriately-formatted byte stream.
Which current Unity API/Library can be used to create HTTP POST requests with a json payload (Content-Type: application/json), to avoid using obsolete components?
UnityWebRequest.Post() does not actually escape correctly text.
Try constructing using UnityWebRequest.Put() and finally reset to UnityWebRequest.Post().
It will work
The default is indeed application/x-www-form-urlencoded, but you can change it.
Try the following
var request = UnityWebRequest.Post(url, json);
request.SetRequestHeader("Content-Type", "application/json");
yield return request.Send();
If you still have no luck with that, there's always the .NET alternative.
I need to convert the Razer template to html conversion in ASP.Net Core.
// TODO: Get html
string body = GetCompiledHtmlStringFromRazor
("~/Views/EmailTemplates/PasswordResetEmailTemplate.cshtml", viewModel);
Any source code for making this process simple.. ?
For .Net use RazorEngine
https://github.com/Antaris/RazorEngine
Use .NetCore use RazorEngine.NetCore
https://github.com/fouadmess/RazorEngine
I'm implementing a web service in ASP.NET Core 2.1 based on a specification that exclusively supports XML. Therefore, the content negotiation process must return a XML document or respond with an error. Unfortunately ASP.NET Core 2.1 supports JSON by default, and by default the content negotiation process always succeeds if a request is made with Accept: application/json.
Does anyone know if it's possible to configure an ASP.NET Core project so that the content negotiation process throws an error if any media type other than XML is set?
Sorry if I am late to the party. This works for me:
services.AddMvc(options =>
{
options.OutputFormatters.RemoveType(typeof(JsonOutputFormatter));
options.InputFormatters.RemoveType(typeof(JsonInputFormatter));
options.ReturnHttpNotAcceptable = true;
})
.AddXmlSerializerFormatters()
.SetCompatibilityVersion(CompatibilityVersion.Version_2_1);
Use MVC input and output formatters:
services.AddMvc(configure =>
{
// remove JSON formatter
var outputFormatters = configure.OutputFormatters;
var jsonOutputFormatter = outputFormatters.First(f => f is JsonOutputFormatter);
outputFormatters.Remove(jsonOutputFormatter);
var inputFormatters = configure.InputFormatters;
var jsonInputFormatter = inputFormatters.First(f => f is JsonInputFormatter);
inputFormatters.Remove(jsonInputFormatter);
}).AddXmlSerializerFormatters()