1

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.

abatishchev
  • 98,240
  • 88
  • 296
  • 433

2 Answers2

0

Change your first route to ( but wild cards might be costy)

routes.MapRoute(
name: "WSContext",
url: "{ws_*}",
defaults: new { controller = "Help", action = "WsContext" }
Dan Hunex
  • 5,172
  • 2
  • 27
  • 38
  • I need to read the controller name in the URL so the **WsContext(string webservice)** method can use it to do the filtering. – Danny Graham May 05 '15 at 21:26
  • Since the routing is done at compile time and you cannot inject action parameter dynamically....your option would be to leave WsContext () without parameter and inside the action use Request.CurrentExecutionFilePath to get the ws_* path – Dan Hunex May 05 '15 at 22:11
  • Thank you Dan. With that said, I'm still curious as to how I can modify the code within WsContext() to render the same view Index() would render without the error mentioned above. – Danny Graham May 05 '15 at 23:10
0

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);
    }