I am working on customizing the ASP.NET Web API help pages.
I am looking for the best approach to display methods that belong to a controller specified by the URL. My controllers are all prefixed with "ws_".
I have added an entry to my RouteConfig to recognize URLs containing the string "ws_" as follows:
public class RouteConfig
{
public static void RegisterRoutes(RouteCollection routes)
{
routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
routes.MapRoute(
name: "WSContext",
url: "ws_{webservice}",
defaults: new { controller = "Help", action = "WsContext" }
);
var route = routes.MapRoute(
name: "Default",
url: "{controller}/{action}/{id}",
defaults: new { controller = "Help", action = "Index", id = UrlParameter.Optional }
);
route.DataTokens["area"] = "HelpPage";
}
}
Here is my HelpController. I'm trying to get the WsContext method to strip out methods that do not belong to the controller (i.e. ws_testController) passed via the URL (i.e. mysite.com/ws_test).
public class HelpController : Controller
{
public HttpConfiguration Configuration { get; private set; }
private Collection<ApiDescription> apiDescriptionCollection;
public HelpController()
: this(GlobalConfiguration.Configuration)
{
}
public HelpController(HttpConfiguration config)
{
Configuration = config;
this.apiDescriptionCollection = Configuration.Services.GetApiExplorer().ApiDescriptions;
}
public ActionResult Index()
{
ViewBag.DocumentationProvider = Configuration.Services.GetDocumentationProvider();
return View(this.apiDescriptionCollection);
}
public ActionResult WsContext(string webservice)
{
ViewBag.DocumentationProvider = Configuration.Services.GetDocumentationProvider();
Collection<ApiDescription> apiCollection = new Collection<ApiDescription>();
foreach (ApiDescription desc in this.apiDescriptionCollection)
{
if (desc.GetControllerName() == ("ws_" + webservice))
apiCollection.Add(desc);
}
if (apiCollection.Count > 0)
this.apiDescriptionCollection = apiCollection;
return RedirectToAction("Index");
}
...
}
I am currently receiving the following error:
No route in the route table matches the supplied values.
I was able to accomplish what I am trying to do with the following.
RougeConfig.cs:
public class RouteConfig
{
public static void RegisterRoutes(RouteCollection routes)
{
routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
var route = routes.MapRoute(
name: "WSContext",
url: "ws_{webservice}",
defaults: new { controller = "Help", action = "Index" }
);
route.DataTokens["area"] = "HelpPage";
route = routes.MapRoute(
name: "Default",
url: "{controller}/{action}/{id}",
defaults: new { controller = "Help", action = "Index", id = UrlParameter.Optional }
);
route.DataTokens["area"] = "HelpPage";
}
}
HelpController.cs
//public ActionResult Index()
//{
// ViewBag.DocumentationProvider = Configuration.Services.GetDocumentationProvider();
// return View(this.apiDescriptionCollection);
//}
public ActionResult Index(string webservice)
{
ViewBag.DocumentationProvider = Configuration.Services.GetDocumentationProvider();
if (!string.IsNullOrEmpty(webservice))
{
Collection<ApiDescription> apiCollection = new Collection<ApiDescription>();
foreach (ApiDescription desc in this.apiDescriptionCollection)
{
if (desc.GetControllerName() == ("ws_" + webservice))
apiCollection.Add(desc);
}
if (apiCollection.Count > 0)
this.apiDescriptionCollection = apiCollection;
}
return View(this.apiDescriptionCollection);
}