Unable to change swagger ui path - asp.net-web-api2

I'm using Swagger / Swashbuckle version 5.6 to generate documentation for my ASP.Net Web API 2 project.
By default API documentation is accessible at URL http://localhost:56081/swagger/ui/index
But, I want it should be available at http://localhost:56081/apihelp/
I searched a lot, tried changing settings in the SwaggerConfig.cs file but nothing seems to make this work.
So, is this even possible? if yes, can anyone help me with this ?

You can add the path to the call of EnableSwaggereUi,
e.g.:
SwaggerConfig.Register(HttpConfiguration config)
{
config.EnableSwaggerUi("apihelp/{*assetPath}", c =>
... // following lines omitted
}
You can then call the UI with the URL http://localhost:56081/apihelp/index
See https://github.com/domaindrivendev/Swashbuckle#custom-routes for reference.
Please note: the default setting 'swagger' redirects automatically to 'swagger/ui/index', but this custom setting does not automaically redirect to 'apihelp/index' when you just use 'apihelp'.
To achieve an automatic redirect you can add the route in WebApiConfig:
config.Routes.MapHttpRoute(
name: "Swagger UI",
routeTemplate: "apihelp",
defaults: null,
constraints: null,
handler: new RedirectHandler(message => message.RequestUri.ToString().TrimEnd('/'), "/index"));
The redirect code is based on v.karbovnichy's answer in How to redirect from root url to /swagger/ui/index?

Related

Configure ASP.NET Core App to set new default files

I went thru the documentation provided for Use Static Files for ASP.NET core app. After Reading information from section Serving a default document & Using the UseFileServer method, following two questions are opened in my mind, respectively:
How can I add new default file if it is outside of wwwroot
How can I add new default file which is even under www using UseFileServer extension method
In your Startup.Configure method you can configure the default file:
DefaultFilesOptions options = new DefaultFilesOptions();
options.DefaultFileNames.Clear();
options.DefaultFileNames.Add("myDefault.html"); // this had no influence :-(
app.UseDefaultFiles(options);
If I define the following options, I can load the default index.html through /StaticFiles but not my customized myDefault.html which is probably what you are after.
app.UseFileServer(new FileServerOptions()
{
FileProvider = new PhysicalFileProvider(#"C:\temp\"),
RequestPath = new PathString("/StaticFiles"),
EnableDefaultFiles = true,
EnableDirectoryBrowsing = false
});
UseDefaultFiles doesn't seem to have any influence. However it should still work if you configure the default file in your web server.
FileServerOptions has a property DefaultFiles, but it is read-only.

How to create custom Routing in ASP.NET Core

I need help with URL rewriting. I am new to ASP.NET Core MVC. When I type anything in {param} part then routing should redirect it to my controller.
So if anyone to types in {param} like
https://mydoamin.com/{param}
then it should be redirected to this url:
https://mydoamin.com/{controller}/{action}/{actionurl}={param}
I found the answer for my question. Just define new custom route in your startup.cs file before your default route.
routes.MapRoute(
"Member", // Route name
"{actionURL}", // URL with parameters
new { controller = "Pages", action = "Details" } // Parameter defaults
);
It's working form me.
We can do the same using this approach.
I find this more convenient.
[Http("add/user/{user}/{password}")]
public IActionResult AddUser(string user, string password)
{
//do your things here...
}
I could recommend you to see this blog post from Stephen Walther: ASP.NET 5 Deep Dive: Routing
I am not sure if this works as you want. https://mydoamin.com/{controller}/{action}/{actionurl}={param} seems not to be a valid URL to me. The part {actionurl}={param} is probably the query part which is comes as a key/value pair and starts always with a ?. You could probably fix your routing if your desired URL would look like https://mydoamin.com/{controller}/{action}/?key1=value1&key2=value2
I don't know still this is useful OR not but in asp.net core 3 this worked for me like a charm.
routes.MapRoute(
"MoviesByReleaseDate",
"movies/released/{year}/{month}",
new { controller = "Movies", action = "ByReleaseDate" }
);

Error enabling swagger with swashbucle

I am trying to create a WebApi project with a couple of simple controllers. If I call the methods using fiddler all is fine, however I prefer to use swashbuckle as it's a bit prettier.
However, with swashbuckle installed using the default configuration it isn't working.
When I navigate to
http://localhost/api/mycontroller/swagger
it redirects to
http://localhost/api/mycontroller/swagger/ui/index
But then it just displays the following error:
<Error>
<Message>
No HTTP resource was found that matches the request URI 'http://localhost/api/Management/swagger/ui/index'.
</Message>
<MessageDetail>
No type was found that matches the controller named 'swagger'.
</MessageDetail>
</Error>
My routing is as follows:
config.MapHttpAttributeRoutes();
config.Routes.MapHttpRoute(
name: "DefaultApi",
routeTemplate: "{controller}/{action}/{id}",
defaults: new { id = RouteParameter.Optional }
);
We found a very simple solution to our issue.
When swagger was added to the project it automatically updated the Bootstrapper.cs within App_Start, and it looked like this:
WebApiConfig.Register(config);
UnityConfig.Register(config, container);
SignalRConfig.Register(app, container);
SwaggerConfig.Register(config);
Simply moving swagger to the top of the list allowed it to register it's routes correctly before the web api routes:
SwaggerConfig.Register(config); // Swagger must be the first thing in this sequence or it just does not work...
WebApiConfig.Register(config);
UnityConfig.Register(config, container);
SignalRConfig.Register(app, container);
Just posting this in the hope that it helps someone else.
I had the same issue, we are only using attribute routing so this may not apply to everyone.
By default the WebApiConfig looks like this:
config.MapHttpAttributeRoutes();
config.Routes.MapHttpRoute(
name: "DefaultApi",
routeTemplate: "{controller}/{id}",
defaults: new { id = RouteParameter.Optional }
);
If you remove the section for to MapHttpRoute for the default routing and just have:
config.MapHttpAttributeRoutes();
It works like a champ!!! I tried following the FAQ regarding fixing it but this was the only thing that worked. Also another item to consider is that I only needed Swashbuckle.Core.

MVC routing under a domain

I have just published an MVC application. It has a Home controller with a method Create.
The URL is http://www.myurl.com
The name of the virtual directory in IIS is myurl.
When I submit the form that posts to Home/Create I'm getting an error because it's trying to send the data to:
http://www.myurl.com/myurl/Home/Create
rather than:
http://www.myurl.com/Home/Create
I tried adding this route:
routes.MapRoute(
"test",
"Home/CreateNewPlayer",
new { controller = "Home", action = "CreateNewPlayer" }
);
but that hasn't made any difference. Can someone please help me out?
You should not host your application in a virtual directory but rather directly inside the website which has a binding to the www.myurl.com domain.

Convert ASP.Net MVC4 site to Azure

I've created a ASP.Net MVC 4 web site that has several custom routes which are registered in the ~/App_Start/RouteConfig file.
public class RouteConfig
{
public static void RegisterRoutes(RouteCollection routes)
{
routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
routes.MapRoute(
name: "Empty",
url: "",
defaults: new { controller = "Home", action = "Index", id = 0 }
);
routes.MapRoute(
name: "Session",
url: "{id}",
defaults: new { controller = "Home", action = "Index" }
);
routes.MapRoute(
name: "Default",
url: "{controller}/{action}/{id}",
defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional }
);
}
}
This site is working great outside of Azure and even works in the Azure emulator, but when I move it up to Azure, it's acting like there are no routes. For instance, when I try to navigate to the root of the web site I get an error stating:
403 - Forbidden: Access is denied.
You do not have permission to view this directory or page using the credentials that you supplied.
This made me think that the route simply wasn't working so I tried going to a another page like http://myapp.cloudapp.net/1234 (which should send 1234 into the home controller's index action. But that action gives me a 404 error.
I've checked my configuration against and new ASP.Net MVC 4 Azure project and they appear to be the same. I've also read through this which gave some pointers but ultimately didn't help.
Any advice on how to troubleshoot this?
Update
This is a WebRole project and is using .Net 4.0 (I already down converted from 4.5).
Have you tried setting a default page as per this article http://www.sunilhaste.com/2011/04/403-forbidden-access-is-denied-error.html
After you have uploaded your Web Role to Azure, you try to access the application and suddenly you get "403-Forbidden, Access denied"
error.
You are able access a page by providing it's complete url ex :- http://xyz.cloudapp.net/Pages/Home.aspx.
You don't have a default.aspx page present at the root level of your website. Solution :- The first thing that you should do is, to
check whether you have added below tag to the web.config file inside
system.webserver tag or not.
<add value="Pages/Home.aspx"/>
I also had had a similar issue and mine was fixed by setting the role to be 2008 R2. Someone in comments to the linked article says
change OS CONFIGURATION from MS SERVER 2008 SP2 to MS SERVER 2008 R2
Also see the following Asp.net and Azure - Can't no longer show my web application suggests checking you have <modules runAllManagedModulesForAllRequests="true" />
Also I assume you are making sure that MVC4 files are being deployed to Azure. Have your RDP'd to the machine and checked event log etc?