我正在定制 ASP.NET Web API 帮助页面。
我正在寻找显示属于 URL 指定的控制器的方法的最佳方法。我的控制器都以“ ws_ ”为前缀。
我在 RouteConfig 中添加了一个条目来识别包含字符串“ ws_ ”的 URL,如下所示:
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";
}
}
这是我的帮助控制器。我试图让 WsContext 方法去除不属于通过 URL(即mysite.com/ws_test )传递的控制器(即ws_testController )的方法。
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");
}
...
}
我目前收到以下错误:
路由表中没有路由与提供的值匹配。