Swagger does not show real error message - asp.net-web-api

We are on .NET WebAPI framework and we are using swagger on top of web api for annotations and out of the box UI experience. So far, it just has been working great. However, when I return an error from WebAPI(Http 400) as following code:
return BadRequest("Some error");
However, when I do that, it seems like Swagger is not able to parse the error properly. It basically, show response 0. Which is wrong for sure because when I go to Chrome DevTools, I see that my request had a response of HTTP 400 as I expected.
How can I configure Swagger so that it is able to show the errors correctly? Or should I be throwing my errors a little bit differently to be able to see it in swagger?

You can annotate your actions like this
[ProducesResponseType(typeof(ResponseObjectTypeHere), 200)]
[ProducesResponseType(typeof(ErrorResponseObjectTypeHere), 400)]
You can also add something like this in the xml documentation if you enable swagger to read and use xml documentation
/// <response code="200">Description</response>
/// <response code="400">Description of error</response>
I used those in the .net core web api project with swagger, should be the same for regular .net web api project as well.

You can simply send the exception details to the client by enabling one of ABP's configurations (SendAllExceptionsToClients) in ***.Web.Core Module, like this:
public override void PreInitialize()
{
if (_env.EnvironmentName.ToLower() == "development")
Configuration.Modules.AbpWebCommon().SendAllExceptionsToClients = true;
}
Then you get the exception details on the client. Recommended only during development.

Related

ASP.NET Core Web API - Exception is not showing immediately in Visual Studio

I have an exception thrown in my ASP.NET Core Web API project.
And I do get the exception name and stacktrace and everything, but the problem is that I get that as a response to the API request, but I want Visual Studio to take me immediately to the exception where and when it was thrown, like I have placed a breakpoint to it.
This is something that used to work in regular ASP.NET Core MVC projects, but for some reason it doesn't work in Web API.
Also, to clarify - I don't have any global exception handling filter or middleware, I just call app.UseDeveloperExceptionPage(); to get the detailed exception in development, this is from my HTTP request pipeline:
if (app.Environment.IsDevelopment())
{
app.UseDeveloperExceptionPage();
app.UseSwagger();
app.UseSwaggerUI();
}
else
{
app.UseCustomExceptionHandler();
}
// Other irrelevant middlewares...
you need to set the Visual Studio like this:
link.
Based on #XinranShen answer I also needed to update the debug settings.
I checked the setting marked below:

Deploy ASP.net core 2.1 WEB API to IIS using Visual Studio Code

Working on an ASP.net core 2.1 web API project. I need to enable the API so that it can be accesed by client applications that we also have under developement.
So far, the only way I've found to publish to IIS is by doing a manual process:
Run dotnet publish -c Release
Copy the files in bin\Release\netcoreapp2.1\publish\ to my IIS Web App folder
I wonder if there is a more straight forward way of doing this.
Also It takes quite sometime to build this release, so for a development environment it's quite a slow process. The problem is that we cannot allow external access to the WEB api when running with F5 on the Integrated test server. How can we enable an more agile testing environment?
Another issue is that when calling for example fetch('MyAPIServer/api/MyItems') from a javascript application, I get a CORS error:
Failed to load http://localhost:86/api/shit: No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin 'http://localhost:8082' is therefore not allowed access. If an opaque response serves your needs, set the request's mode to 'no-cors' to fetch the resource with CORS disabled
Is enabling CORS absolutely necesary when developing this type of apps?
If I fetch like this:
fetch(
`http://localhost:86/api/shit`,{mode: 'no-cors'}
)
I get:
Uncaught (in promise) SyntaxError: Unexpected end of input
at eval (Pos.vue?7f37:68)
As far as the CORs issue goes you can add the following to your startup:
public void ConfigureServices(IServiceCollection services)
{
// Rest of config stuff ...
services.AddCors();
}
Then in you will also need to add the following.
public void Configure(IApplicationBuilder app, IHostingEnvironment env)
{
app.UseCors(builder =>
{
builder.WithOrigins("http://localhost:8080",
"http://localhost:8081",
"http://localhost:8082")
.AllowAnyMethod()
.AllowAnyHeader()
.AllowCredentials();
});
app.UseMvc();
}

How can I disable alert message when the request is fail?

In some request, I need to change this alert to be somewhere on the screen, so how I can disable the default behavior when failing the request the alert show up by abp.message.error and I need to disable it and use another way.
You can send all exception details to the client easily. There's a setting for this purpose.
...
using Abp.Web.Configuration;
...
public override void PreInitialize()
{
Configuration.Modules.AbpWebCommon().SendAllExceptionsToClients = true;
}
...
References:
Related Aspnet Boilerplate document
Throwing user friendly exception forum post
Related GitHub commit
Yet another related GitHub issue
Besides, you can disable exception handling for an application service or for an application service method. Just use [DontWrapResult] attribute for that.
public interface ITestAppService : IApplicationService
{
[DontWrapResult]
DoItOutput DoIt(DoItInput input);
}
See the related Aspnet Boilerplate docs

ABP BoilerPlate: i cant see my exception message when i'm using UserFirendlyException

i'm using ABP ASP boilerplate .net core / angular client side frame work.
i want to show specific message when some condition exists in server side by raising UserFriendlyException("myMessage") but in client side only showing popup window
my end point API signature is like this async Task<BDto> CalcBodies(ADto input)
my problem solved by specifying LocalizationSourceName property of Service instance base on this post : GitHub link

Kendo UI Remote DataSource

This is with reference to a question posted on the Kendo UI DataSource forum by a colleague of mine. I'm cross posting it here to try to get a solution.
http://www.telerik.com/forums/failure-to-read-remote-datasource
We are trying out this simple Kendo Dojo using a remote URL to populate the datasource (open console to see the ouput). While modifying this to use one of our data URLs (and using json as the datatype), it doesn't work. Its an internal URL, so its not accessible from SO but I'll post the request/response headers, and the data seen using Chrome's Developer tools. The data source is making the request to the URL and getting a response, but nothing happens after that.
Response data
[{"tradeVolume": "33454327",
"numberOfTrades": "130498",
"tradeSizeStdDev": 13048.15096724034,
"quoteSizeStdDev": 1306.394869511115,
"medianTrade": 100.0,
"eightiethTradeSize": 200.0,
"ninetiethTradeSize": 334.2000000000029,
"ninetyFifthTradeSize": 334.2000000000029,
"ninetyEighthTradeSize": 1100.0,
"ninetyNinthTradeSize": 2000.0,
"medianQuoteSize": 1000.0,
"eightiethQuoteSize": 1550.0,
"ninetiethQuoteSize": 2050.0,
"ninetyFifthQuoteSize": 2050.0,
"ninetyEighthQuoteSize": 4300.0,
"ninetyNinthQuoteSize": 5600.0,
"ninetyNinthFiveQuoteSize": 7850.0
}]
Request/Response Headers
Can't figure out why the datasource doesn't like the response. If we use the same response as a local datasource, it works, so I don't think there is an issue with the format of the JSON.
Also, any pointers on best ways to debug such issues would be very helpful to us.
UPDATE: This is an updated Dojo example that shows the code we have been trying. We have removed the URL since its an internal once, but the request/response headers are the ones seen above.
http://dojo.telerik.com/#juliayu/apEHA

Resources