How to change base url of Swagger in ASP.NET WebApi - asp.net-web-api

By default when you enable swagger in ASP.NET WebApi project is available on URL:
http://localhost:<random_port>/swagger/UI
I would like to use http://localhost:<random_port>/user instead of http://localhost:<random_port>/swagger/UI to access swagger. How/where can I configure that?
I found that for Asp.net core versions we can configure using RoutePrefix, but is there any similar method in ASP.NET Web API:
app.UseSwaggerUI(c =>
{
c.RoutePrefix = "users";
});

Related

Modify Web API action name generated by Swagger

I have a Web API project which is integrated with Swagger. I am using a low code platform IDE OutSystems to consume the REST API.
Outsystems IDE takes following URL to generate REST methods:
http://10.0.0.11/PortalService/swagger/docs/v1
I have the following action method in my web API project:
[HttpGet]
[Route("api/LoanApplications")]
public IHttpActionResult AllLoanApplicationsByUser(string userID, [FromUri]List<string> lstInstitutionId)
For this method Swagger is generating following method name:
Dashboard_AllLoanApplicationsByUser
How to configure Swagger to generate method name similar to the action name from code?
Note: Dashboard is the name of the controller.
Okay. I found the answer here.
This is what I was looking for:
[SwaggerOperation("MethodName")]

ASP.Net Core Web API - ResponseCache attribute is not adding "Cache-Control" header to the reponse

I have multiple controllers in my ASP .NET Core application and I am using ReponseCache attribute like this on a few methods:
//controller
[Route("api/[controller]")]
[EnableCors("CorsPolicy")]
public class InsightsApiController : Controller
//method
[Route("CoursesTextContent")]
[HttpGet]
[DecryptFilter]
[ResponseCache(Duration = 60)]
public IActionResult GetCoursesContent(string locale, string tabKey, string widgetType)
The issue that I am having is that for one controller this is working fine and I can see the response in chrome dev tools with "Cache-Control:public max-age=60" but in a different controller when I add this attribute its adding "Cache-Control:no-cache". I compared both controllers and methods in them and they are configured same. I have also tried to add ASP.NET Core middleware recommended here but same results. I am calling both methods from Angular2 webpage. Is there something I can do from the client side (request)? or something in the ASP.NET Core app setup?
You are missing a trailing parenthesis
[ResponseCache(Duration = 60]
needs to be
[ResponseCache(Duration = 60)]
I had a session middleware enabled in the startup.cs file of my ASNETCore webapi project. I removed it and its working for all calls/controllers now. Not sure why it was causing problem only with one Controller.

How do I use FluentValidation alongside Swagger in .NET Web API application?

I have a .NET Web API project. I was able to add Swagger by installing the Swashbuckle NuGet package. Swagger now makes a nice web page that provides documentation and ability to test by going to this url:
http://localhost:20475/product-catalog-api/swagger/ui/index
This part works great. However, when I add FluentValidation and set it up in the WebApiConfig.cs, all of the requests to my site get high-jacked and instead of seeing the Swagger html page, I only get a JSON with an empty response.
I used this article by Matthew Jones to set up FluentValidation:
http://www.exceptionnotfound.net/custom-validation-in-asp-net-web-api-with-fluentvalidation/
Here is what my web api config looks like:
public static void Register(HttpConfiguration config)
{
// Web API configuration and services
config.Filters.Add(new ValidateModelStateFilter());
config.MessageHandlers.Add(new ResponseWrappingHandler());
// Web API routes
config.MapHttpAttributeRoutes();
config.Routes.MapHttpRoute(
name: "DefaultApi",
routeTemplate: Version + "/{action}",
defaults: new {controller = "Main"}
);
FluentValidationModelValidatorProvider.Configure(config);
}
I could trigger the FluentValidation manually in every web service method, but I would like to have a generic solution like Matthew Jones did with the Filters and MessageHandlers. Basically what I want to do is say IF the request is for swagger-ui then do not route through FluentValidation.
I ended up adding this code inside ResponseWrappingHandler. There may be a more elegant way, but this worked for me. This causes the swagger page request to kick out of the FluentValidation hook.
if (request.RequestUri.AbsolutePath.Contains("swagger"))
{
return response;
}

Orchard CMS. IAutofacActionFilter

I need to inject services into WebApi Action Filter (or into DelegatingHandler).
With Autofac I can use IAutofacActionFilter (https://code.google.com/p/autofac/wiki/WebApiIntegration). But there isn't Autofac's WebApi Dependecy Resolver in the Orchard. There is Orchard's resolver implementation.
Also I tried this:
var service = (IMyService)GlobalConfiguration.Configuration.DependencyResolver.GetService(typeof(IMyService));
or
var service = (IMyService)request.GetDependencyScope().GetService(typeof(IMyService));
But it doesn't work too.

MVC Web API not working with Autofac Integration

I used the MVC integration from autofac like this:
...
var container = builder.Build();
DependencyResolver.SetResolver(new AutofacDependencyResolver(container));
But now I want to recreate the solution with the new Web Api RTM.
And I want to use the new AutofacWebApiDependencyResolver class.
But if I do this with AutofacWebApiDependencyResolver i got this error:
The type Autofac.Integration.WebApi.AutofacWebApiDependencyResolver
does not appear to implement
Microsoft.Practices.ServiceLocation.IServiceLocator.
I read that i have to do this now for setting the resolver:
GlobalConfiguration.Configuration.DependencyResolver = new AutofacWebApiDependencyResolver(container);
But if I set it with '=', it is not set. Its still the DefaultResolver...
If I use the MVC AutofacDependencyResolver class again, it works.
Are were still problems with autofac and web api rtm? (the current integration is RC version)
ASP.Net Wep.API and ASP.NET MVC uses two different IDependencyResolver (because they designed the Wep.API to not depend on ASP.NET MVC) so you need to setup both:
var container = builder.Build();
DependencyResolver.SetResolver(new AutofacDependencyResolver(container));
GlobalConfiguration.Configuration.DependencyResolver =
new AutofacWebApiDependencyResolver(container);
So the AutofacDependencyResolver is needed to inject decencies to regular MVC Controller derived controllers.
And the AutofacWebApiDependencyResolver is needed to inject dependencies to the Web.API ApiController derived controllers.
And don't forget to register your Api controllers in your container builder with the call (it differs from usual builder.RegisterControllers method):
builder.RegisterApiControllers(Assembly.GetExecutingAssembly());
Following is a very good example
Using Autofac with Web Api

Resources