如何仅显示属于URL

时间:2015-05-05 21:04:07

标签: c# asp.net-mvc asp.net-web-api asp.net-web-api-routing asp.net-web-api-helppages

我正致力于自定义ASP.NET Web API帮助页面。

我正在寻找显示属于URL指定的控制器的方法的最佳方法。我的控制器都以“ ws _ ”为前缀。

我在RouteConfig中添加了一个条目,用于识别包含字符串“ ws _ ”的网址,如下所示:

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

这是我的HelpController。我正在尝试使用WsContext方法去除不属于控制器的方法(即 ws_testController )通过URL传递(即 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");
    }
...
}

我目前收到以下错误:

  

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

2 个答案:

答案 0 :(得分:0)

将您的第一条路线更改为(但外卡可能很省钱)

routes.MapRoute(
name: "WSContext",
url: "{ws_*}",
defaults: new { controller = "Help", action = "WsContext" }

答案 1 :(得分: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";
    }
}

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