How to navigate directly to specific version of API in swagger-ui via Swashbuckle - asp.net-web-api2

I want to link to the swagger doc for specific API versions from a separate site. Can I pre-select the DiscoverySelector using a url route?
For example, it'd be great if I could do something like this:
<ul>
<li> My API 1.0</li>
<li> My API 2.0</li>
</ul>
I am using Web API 2, so I have to use the Swashbuckle.

Yes you can, and this is a feature of Swagger-ui not Swashbuckle:
Here are examples:
http://petstore.swagger.io/?url=http://swagger-net-test-multiapiversions.azurewebsites.net/swagger/docs/V1_0
http://petstore.swagger.io/?url=http://swagger-net-test-multiapiversions.azurewebsites.net/swagger/docs/V2_0
Here is the multiversion site:
http://swagger-net-test-multiapiversions.azurewebsites.net/swagger/ui/index

Related

swagger API documentation with my own yaml file

I have my API documented with Swagger. For developer convenience I would like to provide the swagger GUI on my website as well. However, my provider has not installed the php yaml extension. It implies that I can't use the GUI on my own website.
So, I would like to use a third party GUI. I know that I can use https://petstore.swagger.io/ and enter the link to my yaml file in the text box. This is also not really user friendly. I prefer to open the GUI and specify the yaml when calling the url. For the user the GUI opens with my API definition.
Any thoughts?
If for some reason you cannot host Swagger UI youself, here are some alternatives you can try:
Use SwaggerHub to host your API definition and docs.
Disclosure: I work for the company that makes SwaggerHub.
Use GitLab to host your OpenAPI YAML/JSON file. GitLab uses Swagger UI to render OpenAPI files. Example:
https://gitlab.com/gofus/gofus-api/blob/dev/swagger.yaml
Use https://petstore.swagger.io with the url query parameter to automatically load your API definition:
https://petstore.swagger.io?url=https://yoursite.com/api.yaml
For this to work, the server where your OpenAPI file is hosted must use HTTPS and support CORS.

Change the "/api" part of the url in ASP.Net Core

When I run a .NET Core Web API project, I get a URL that looks like this: http://localhost:5000/api/...
How can I change the /api/ part of the URL to something else? i.e. - http://localhost:5000/myservice/...
I am using Kestrel as my web host.
It depends on how you have the project setup. By default I believe it uses attribute routing. In your controller you should see something like this
[Route("api/[controller]")]
public class ValuesController : Controller
{
Where the [Route("api/[controller]")] would just need to be changed to [Route("myservice/[controller]")]
If you wanted to do it Globally you could do it like this.
app.UseMvc(routes =>
{
routes.MapRoute("default", "myservice/{controller=values}/{action=get}/{id?}");
});
Although I myself don't use this, and it's not exactly what MS Recommends for a Web Api. You can read more here.
Mixed Routing
MVC applications can mix the use of conventional routing and attribute routing. It's typical to use conventional routes for controllers serving HTML pages for browsers, and attribute routing for controllers serving REST APIs.
You could just install microsoft.aspnetcore.http.abstractions nuget package and use UsePathBaseExtensionsextension method on IApplicationBuilder in your Startup.Configure method like so:
app.UsePathBase("/api/v1");

How to use ASP.Net MVC4 Web API with AngularJS partial views (aka ng-include)

Is it possible to return '*.htm' files from the ASP.Net MVC4 Web API application?
I am trying to use AngularJs to manage the client side views and when I include the following commend on my page:
<div ng-controller="application.RootController">
<div ng-switch="subview" >
<div ng-switch-when="home" ng-include="'../Views/Angular/customer/overview.htm'"></div>
<div ng-switch-when="admin" ng-include="'../Views/Angular/Admin/home.htm'"></div>
</div>
</div>
I see that this is working as expected on the browser, but the ASP.Net is returning 404 page not found.
Also I am unable to physically browse any of the htm pages, so is there a special thing i need to do in ASP.Net MVC in terms of configuring the routing to return out the htm pages?
Files in the ~/Views folder are not served directly. You should place your HTM pages somewhere else. For example you could have an ~/Angular folder.
your should place static files into '/static' folder
You can call razor #html.partial to display your partial views
<div ng-controller="application.RootController">
<div ng-switch="subview" >
<div ng-switch-when="admin"> #Html.partial("~/Views/Angular/Admin/home.htm")</div>
</div>
</div>

How can I identify web requests created when someone links to my site from Google+?

To comply with new EU legislation regarding cookies, we've had to implement cookie 'warning' banners in several places on our site. We're now seeing problems when users try and link/embed content from our site on Facebook, Google+, bit.ly and so on - the embedded page thumbnail is showing the cookie notification banner instead of a preview of the actual page.
Most of these sites use a distinctive user agent string, so we can easily identify their incoming requests and disable the cookie banner - but Google+ seems to identify itself as
Mozilla/5.0 (Windows NT 6.1; rv:6.0) Gecko/20110814 Firefox/6.0
which makes it very difficult to distinguish between Google+ automated requests and browsing traffic from 'real' users. Is there any way at all I can identify these requests? Custom HTTP headers I can look for or anything?
G+ has got its user agent. It contains the text: Google (+https://developers.google.com/+/web/snippet/).
Ref: https://developers.google.com/+/web/snippet/#faq-snippet-useragent
There are not any HTTP headers that you can depend on for detecting the +1 button page fetcher, but there is a feature request for it to specify a distinctive user agent. In the mean time, I would not depend on it.
You can, however, use other means to configure the snippet of content that appears on Google+. If you add structured markup such as schema.org or OpenGraph to your pages, the +1 button will pull the snippet from those tags. Google+ provides a configuration tool and docs to help you design your markup.
If you add schema.org markup it might look something like this:
<body itemscope itemtype="http://schema.org/Product">
<h1 itemprop="name">Shiny Trinket</h1>
<img itemprop="image" src="http://example.com/trinket.jpg" />
<p itemprop="description">Shiny trinkets are shiny.</p>
</body>

REST support within a java templating engine such as StringTemplate, FreeMarker, Velocity or Tiles?

I'd like to compare some templating engines that supports creating RESTful URLS for templating header/body/footer pages in a java application. I don't want my pages to have a jsp, .st or .ftl extension. Does anyone have links to example applications that illustrate how to set up REST with any of the popular templating engines?
Thanks.
-John
It's not a problem with any of the mentioned engines. They only provide the MVC View. The visited URL belongs to the MVC Controller (the "action"). Thus, the page URL should never contain the template file name. (In JSP Model-2 frameworks you forward the HTTP request to the view page, so in that sense the templates have an URL. But this request forwarding is entirely server side, so the template URL is still not visible on the client side.) If the MVC Controllers are JSP pages, you can still hide the .jps extension by creating a catch-all central controller servlet (or filter) that adds the .jsp extension to the URL and forwards the HTTP request internally.