I have a C# .Net 4.5 Web Api application to which I have added a Help Page such as the one shown here.
When a developer launches the Web Api application in Visual Studio, I would like the help page to come up.
I would like to accomplish this by the using routing (such as a change to WebApiConfig.cs or Global.asax.cs) as opposed to a setting in the project's properties.
In the WebApiConfig.cs file I tried adding the following -
config.Routes.MapHttpRoute("Default", "api/help");
That did not work. Does anyone know how to make this work? Thanks.
The Default route maps the first segment of a URL to a controller name, the second segment of a URL to a controller action, and the third segment to a parameter named id.
Web API uses URI as “DomainName/api/ControllerName/Id” by default where Id is the optional parameter. If we want to change the routing globally, then we have to change routing code in register Method in WebApiConfig.
Two years late, but for the benefit of Googlers like myself - A way to do it (based on this answer) is simply to modify the RegisterArea
method in the HelpPageAreaRegistration.cs
class in the HelpPage Area to contain a blank route. Example -
public override void RegisterArea(AreaRegistrationContext context)
{
context.MapRoute(
"HelpPage_Default",
"Help/{action}/{apiId}",
new { controller = "Help", action = "Index", apiId = UrlParameter.Optional });
context.MapRoute(
"Help Area",
"",
new { controller = "Help", action = "Index" });
HelpPageConfig.Register(GlobalConfiguration.Configuration);
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With