How to disable default error response for Api Versioning - c#

Good evening, everybody. Trying to implement Api Versioning for my project. Faced with the problem that receiving default errors template response for request. Look code 1
{
"error": {
"code": "UnsupportedApiVersion",
"message": "The HTTP resource that matches the request URI 'https://localhost:5003/v2.3' does not support the API version '2.3'.",
"innerError": null
}
}
But I have my own error response template that want to receive.
{
"traceId": "0HMF1NONVN8SF:00000002",
"errors": [
{
"message": "",
"source": ""
}
]
}
As I understand I could implement my own ErrorResponseProvider, but could I avoid doing that?
How could I can disable ErrorResponse for ApiVersionOptions?
My api version configuration:
services
.AddApiVersioning(opt =>
{
opt.ApiVersionReader = new UrlSegmentApiVersionReader();
opt.UseApiBehavior = false;
opt.DefaultApiVersion = ApiVersion.Default;
opt.AssumeDefaultVersionWhenUnspecified = true;
})
.AddVersionedApiExplorer(opt =>
{
opt.GroupNameFormat = "'v'VV";
opt.SubstituteApiVersionInUrl = true;
});
Versions:
.NET 6
ASP.NET Core 6
Microsoft.AspNetCore.Mvc.Versioning 5

Actually I didn't find the way to disable it. However, I found the solution for my case. First of all, I implemented my CustomErrorResponseProvider for ErrorResponses.
public class CustomErrorResponseProvider : DefaultErrorResponseProvider
{
public override IActionResult CreateResponse(ErrorResponseContext context)
{
switch (context.ErrorCode)
{
case "UnsupportedApiVersion":
throw new UnsupportedApiVersionException("Url", context.Message);
}
return base.CreateResponse(context);
}
}
Than started throw necessary exception from it. And that's all!
As my middleware looks like this(img below), my ErrorHandlingMiddleware didn't catch an exception, because it was generated by default api versioning error handler.

Related

Is it possible to call an Dynamics CRM action with a JSON request through a WebApi with IOrganizationService?

TL;DR:
I am calling a WebApi, the WebApi authenticates against the CRM and use the IOrganizationService, so my request is a JObject and not an Entity or EntityReference, it gives me this error:
Error: Type 'Newtonsoft.Json.Linq.JToken' is a recursive collection data contract which is not supported. Consider modifying the definition of collection 'Newtonsoft.Json.Linq.JToken' to remove references to itself.
Context:
I built a web application in angular and I built a WebApi so I can call some custom actions in CRM:
Angular APP | WebApi | OnPremise CRM
So, when I call the WebApi, there is a controller that turns my request into a OrganizationRequest:
Request for WebApi:
{
"ActionName": "custom_actionname",
"Parameters":
[
{
"Key": "EntityInputParameter1",
"Value": {"#odata.type":"Microsoft.Dynamics.CRM.any_entity"}
}
]
}
I read this request on my WebApi and turn that into a request for CRM
Request for CRM:
OrganizationRequest request = new OrganizationRequest("custom_actionname");
request.Parameters["EntityInputParameter1"] = {"#odata.type":"Microsoft.Dynamics.CRM.any_entity"} // This is a JObject
OrganizationResponse response = service.Execute(request);
When I make the request, it gives me the following error:
Error: Type 'Newtonsoft.Json.Linq.JToken' is a recursive collection data contract which is not supported. Consider modifying the definition of collection 'Newtonsoft.Json.Linq.JToken' to remove references to itself.
If I make the request directly to the action it works, but I cannot do that due security policies.
One option could be turn the request into a valid CRM request (parsing {"#odata.type":"Microsoft.Dynamics.CRM.any_entity} into a Entity type) but CRM has a lot of parsing escenarios and could be very complex.
Another option could be sending the request through web and stop using the IOrganizationService but I cannot change that.
I am making this question so anybody that has this error can find the "solution" because I searched a lot and nobody refers this behavior directly.
I am probably turning my InputEntityParameter into string, and I will send the JSON, so I can parse the JSON on my action, but I was looking if anybody else had this error or another approach.
I tested it on one of my Dev Environment with Entity as Parameter.
Below is the code I used in console application to fire Action with Entity as parameter. It ran successfully
var request = new OrganizationRequest("new_test");
//request.Parameters.Add("Target", xAccountReference);
request.Parameters.Add("Param2", "abc");
request.Parameters.Add("Param1", new Entity("account",Guid.Parse("2fe32f22-d01d-ea11-80fa-005056936c69")));
Service.Execute(request);
Below is the Javascript code which used CRM Webapi to execute Action with Parameter. Ignore the XRM.Webapi command but interesting for you would be passing parameters in webapi.
var parameters = {};
parameters.Param2 = "abcd";
var param1 = {};
param1.accountid = "2fe32f22-d01d-ea11-80fa-005056936c69"; //Delete if creating new record
param1["#odata.type"] = "Microsoft.Dynamics.CRM.account";
parameters.Param1 = param1;
var new_testRequest = {
Param2: parameters.Param2,
Param1: parameters.Param1,
getMetadata: function() {
return {
boundParameter: null,
parameterTypes: {
"Param2": {
"typeName": "Edm.String",
"structuralProperty": 1
},
"Param1": {
"typeName": "mscrm.account",
"structuralProperty": 5
}
},
operationType: 0,
operationName: "new_test"
};
}
};
Xrm.WebApi.online.execute(new_testRequest).then(
function success(result) {
if (result.ok) {
//Success - No Return Data - Do Something
}
},
function(error) {
Xrm.Utility.alertDialog(error.message);
}
);
I can confirm that you are mixing Webapi and orgservice call. You can definitely call Action from Webapi of Dynamics. I just used Postman to call Action and I was successful. Blog reference to use Postman for CRM webapi
Below Body as json in Postman and I get Action to run.
{
"Param1":"string test",
"Param2":{
"accountid":"b6b35fd0-b9c3-e311-88e2-00505693000c",
"#odata.type":"Microsoft.Dynamics.CRM.account"
}
}

ASP.NET core error handling error message

I am starting to work on ASP.NET core error handling, and I noticed that I am getting an error in a format
{
"errors": {},
"type": "https://tools.ietf.org/html/rfc7231#section-6.5.1",
"title": "One or more validation errors occurred.",
"status": 400,
"traceId": "|caeb2316-47fb93bd8a17bc0a."
}
I am trying to get a more detailed error message, but I cannot get rid of this message.
I just followed instruction on Handle errors in ASP.NET Core, but error is always there.
How can I get rid of this format of error message?
Using the below code you can create custom error response or custom problem details:
services.AddControllers()
.ConfigureApiBehaviorOptions(o =>
{
o.InvalidModelStateResponseFactory = context =>
{
var problemsDetailsFactory = context.HttpContext.RequestServices
.GetRequiredService<ProblemDetailsFactory>();
var problemDetails = problemsDetailsFactory.CreateValidationProblemDetails(
context.HttpContext,
context.ModelState);
problemDetails.Detail = "Custom Details";
problemDetails.Instance = context.HttpContext.Request.Path;
problemDetails.Type = "https://tools.etf............";
//problemDetails.Status = StatusCodes.Status422UnprocessableEntity;
problemDetails.Status = StatusCodes.Status400BadRequest;
problemDetails.Title = "One or more errors on input occured";
return new BadRequestObjectResult(problemDetails)
{
//ContentTypes= {"application/custom+json"}
};
};
});

Displaying Custom Server Side Error Message in Angular and .Net Core API

I have some rest API written in C# and the API is called from Angular (I am using version Angular 8). The call is fine and it is working fine. However, in case of any exception, I cannot display the customized error message in angular. For example, suppose I have a server side validation in C# which validates if the value of a field matches with the string "abc". If it does not match, it will throw an error and in UI (developed in Angular), I want to display the message
"Invalid String Specified".
My server side code is as below -
if (headerValues.Equals("abc")) {
throw new InvalidStringException("Invalid String specified", 999);
}
The Invalid InvalidStringException class is as below -
public class InvalidStringException : System.Exception
{
int status { get; set; }
public InvalidStringException() { }
public InvalidStringException(string message, int status) : base(message) {
this.status = status;
}
}
When that exception is thrown and caught in server side, it is available as 500 exception but could not print the custom message.
I am trying following code in Angular -
} catch (error) {
console.log("Error Status: ", error.status);
console.log("Error Status: ", error.message);
}
Please suggest how to handle that scenario.
The error object that your Angular app receives should be an instance of HttpErrorResponse
You could do something like this to handle http errors:
if (error instanceof HttpErrorResponse) {
if (!error.status) {
console.log(error.message || error.toString());
} else {
console.log(`error status : ${error.status} ${error.statusText}`);
switch (error.status) {
case 401:
this.router.navigateByUrl("/login");
break;
case 500:
this.router.navigateByUrl("/login");
console.log(`redirect to login`);
break;
}
}
} else {
console.error("Other Errors");
}
You are throwing an exception which is handled by C# exception handler and it will only return the custom error message specified in that handler.
To return a custom message, you need to return with http code like 4xx or 5xx.
new HttpResponseException(Request.CreateErrorResponse(System.Net.HttpStatusCode.Conflict, "Custom Message"));
Or you can return with 2xx and you have to parse this subscribe or then method e.g.
new System.Web.Http.Results.ResponseMessageResult(
Request.CreateResponse((HttpStatusCode)227, "Custom Error Message")
);
this.http.get().toPromise().then((response: any) => {
if (response.status == 227) {
return error;
} else {
return data;
}
return apiResponse;
}).catch(error => {
//nothing here
});
If throwing a exception is not really necessary, you can return status code 400 and a message using BadRequest:
if (headerValues.Equals("abc")) {
return BadRequest("Invalid String specified");
}
Are you explicitly catching the InvalidStringException in your .NET API controller and returning the custom message? If not, the response will be a generic HTTP 500 'Internal Server Error' response. I'd suggest explicitly catching the InvalidStringException in your .NET API controller and returning a 400 response with your custom message e.g.
try {
...
}
catch (InvalidStringException iex) {
return BadRequest(iex.message); // iex.message == Invalid String specified
}
When the InvalidStringException scenario occurs, This will return a HTTP 400 response with "Invalid String specified" as the response body. You should be able to log the error on Angular side as you're currently doing...
As other people have mentioned, you need to catch the exception and convert it to an appropriate HTTP response in your own code.
The reason for that is because if otherwise your exception is handled by ASP.NET Core using exception handling configuration you have, and it may vary:
With developer exception page
Usually in development, you will have code:
if (env.IsDevelopment())
{
app.UseDeveloperExceptionPage();
}
What it does is when your environment is Development, it turns on a special page for developers to see information of unhandled exceptions. It is only in this case, you get the exception stacktrace as well as the exception message in the response body.
Without developer exception page
Conversely, if the exception page is turned off (usually off for Production environment), you will see nothing in the response body.
How to fix
Given exception handling in ASP.NET Core is a cross-cutting concern, I wouldn't use try...catch around everywhere InvalidStringException needs to be converted to HttpResponse.
I would instead use either an IActionFilter or use UseExceptionHandler which is the exception handling middleware:
Here is an example of using UseExceptionHandler in Configure method in Startup.cs:
app.UseExceptionHandler(opt => opt.Run(
async ctx =>
{
var feature = ctx.Features.Get<IExceptionHandlerFeature>();
if (feature.Error is InvalidStringException ex)
{
await ctx.Response.WriteAsync(ex.Message);
}
}));
In this way, your InvalidStringException is handled globally in your application, without explicit try...catch. And you could throw the exception anywhere in your code, the above code would catch the exception and properly convert it to an HTTP response with your own message as the body.
Also note, because you are calling the API from an Angular app, so chances are you might need to set CORS up in your API application if the two applications run from different origins.
Without CORS, your HTTP request from the Angular app may fail before it can reach your API. In this case, the status of the HTTP response in your Angular app may be undefined. And in your console, you could see CORS errors.
you can use http interceptor to create general error handler for all http error in angular app,this way you can use alert ,redirect to login page in case token expired ,overwrite the error object and more but you can still access to the error object at the component level by add a callback for observable error.
Error Handler Service
#Injectable()
export class ErrorHandlerService implements HttpInterceptor {
constructor(private msgServ: MessageService) {}
public intercept(
req: HttpRequest<any>,
next: HttpHandler
): Observable<HttpEvent<any>> {
return next.handle(req).pipe(
catchError((err: HttpErrorResponse) => {
switch (err.status) {
case 500: {
this.msgServ.add({
severity: "error",
summary: "Error ",
detail: "Server is gone..💀"
});
break;
}
case 400: {
this.msgServ.add({
severity: "error",
summary: "Error ",
detail: "custome error message..."
});
break;
}
case 401: {
if (err.message == "invalid_token") {
// router 👉 navigate to login
}
break;
}
default: {
this.msgServ.add({
severity: "error",
summary: "Error ",
detail: err.message
});
}
}
return throwError(err);
})
);
}
}
add the Interceptor to Providers in app module
#NgModule({
....
providers: [
{ provide: HTTP_INTERCEPTORS, useClass: ErrorHandlerService, multi: true },
MessageService
],
....
})
export class AppModule {}
demo 🚀
MessageService is related to primeng component library ,you can use your own alert structure

How to configure and use Microsoft.AspNetCore.JsonPatch in ASP.NET Core 2.0 Web API

I am creating an HTTP Partial method in my ASP.NET Web API controller and I read this document http://benfoster.io/blog/aspnet-core-json-patch-partial-api-updates on how to achieve HTTP Partial methods in a controller. I get an exception when I hit the HTTP Partial endpoint that says
Here is my code for the Patch method in the controller:
[HttpPatch("{userId}")]
public IActionResult Patch([FromRoute(Name = "userId")]Guid userId, [FromBody] JsonPatchDocument<User> userProperties)
{
var indexOfUserToPartiallyUpdate = UsersInMemory.List.FindIndex(user => user.Id == userId);
if (indexOfUserToPartiallyUpdate == -1)
{
return BadRequest($"user with {userId} not found.");
}
var originalUser = UsersInMemory.List[indexOfUserToPartiallyUpdate];
userProperties.ApplyTo(UsersInMemory.List[indexOfUserToPartiallyUpdate], ModelState);
if (!ModelState.IsValid)
{
return new BadRequestObjectResult(ModelState);
}
var model = new
{
beforePatch = originalUser,
afterPatch = UsersInMemory.List[indexOfUserToPartiallyUpdate]
};
return Ok(model);
}
And here is the JSON body I'm sending through postman in the HTTP PATCH request:
I feel like I need to do something in the Startup.cs file such as configuring the JsonPatchDocument but I don't know how. Any help is much appreciated.
I think i found your issue: "Note that we always send an array of operations even if you're only sending a single operation."
Try to change your request in:
[
{
"op": "replace",
"path": "/email",
"value": "THIS_SOME_OTHER_EMAIL#gmail.com"
}
]

Google+ Moments:Insert AddActivity generic error

I'm trying to publish an app moment for Google+. The app successfully completes the authentication process and receives the access token. The problem happens when I try to call the Moment.Insert API on "https://www.googleapis.com/plus/v1/people/me/moments/vault", always returns this unspecified error message
{
"error": {
"errors": [
{
"domain": "global",
"reason": "invalid",
"message": "Invalid Value"
}
],
"code": 400,
"message": "Invalid Value"
}
}
The API steps I'm following are the ones listed here https://developers.google.com/+/api/latest/moments/insert and the request looks like this
{
"target": {
"kind": "plus#itemScope",
"name": "Sample Test for Google Plus REST API",
"id": "88370F18-81A4-47F5-BC2F-19EDBE326A20",
"url": "http://schema.org/Thing"
},
"type": "http://schemas.google.com/AddActivity",
"kind": "plus#moment"
}
I also add the access token to the request header & the api key to the url as a parameter.
I don't know if I'm missing something here but it may worth saying that I receive the exact same error message when using the API explorer in Google Dev. Console.
Thanks in advance for any insights
This is the moment declaration that worked for me. For some reason, the description field is required and not adding it would produce this strange error message.
var client = new RestClient();
var moment = new
{
kind = "plus#moment",
type = "https://schemas.google.com/AddActivity",
target = new
{
kind = "plus#itemScope",
id = "GENERATE-A-POST-ID-HERE",
type = "https://schemas.google.com/AddActivity",
name = "SOMTHING-TO-POST",
description = "SOMTHING-TO-POST"
}
};
var request = new RestRequest(string.Format("{0}?key={1}", GOOGLE_POST_URL, GOOGLE_APP_ID), Method.POST);
request.AddHeader("authorization", "BEARER-ACCESS-TOKEN-RECEIVED-BEFORE");
request.AddBody(moment);
client.ExecuteAsync(request, OnPostCompleted);
I'm using RestSharp library to handle the REST operations.
Hope this would help.

Categories