1

我正在定制 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");
    }
...
}

我目前收到以下错误:

路由表中没有路由与提供的值匹配。

4

2 回答 2

0

将您的第一条路线更改为(但通配符可能很昂贵)

routes.MapRoute(
name: "WSContext",
url: "{ws_*}",
defaults: new { controller = "Help", action = "WsContext" }
于 2015-05-05T21:14:56.693 回答
0

我能够通过以下方式完成我想要做的事情。

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

帮助控制器.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);
    }
于 2015-05-06T14:41:53.543 回答