【问题标题】:How do I only display methods that belong to a controller specified by the URL如何仅显示属于 URL 指定的控制器的方法
【发布时间】:2015-05-06 22:41:53
【问题描述】:

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

我目前收到以下错误:

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

【问题讨论】:

  • 我不确定我是否理解这个问题。我相信 Web API 是基于 MVC 构建的?
  • 不,在 ASP.NET 6 之前,它们是相似但在技术上不同的框架。
  • 谢谢。很高兴知道。我想这个问题属于 Web API。
  • 但是 Web API 使用继承自 ApiController、MVC - Controller 的控制器。

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


【解决方案1】:

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

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

【讨论】:

  • 我需要读取 URL 中的控制器名称,以便 WsContext(string webservice) 方法可以使用它进行过滤。
  • 因为路由是在编译时完成的,你不能动态注入动作参数......你的选择是让 WsContext () 不带参数,在动作内部使用 Request.CurrentExecutionFilePath 来获取 ws_ * 路径
  • 谢谢丹。话虽如此,我仍然很好奇如何修改 WsContext() 中的代码以呈现与 Index() 相同的视图,而不会出现上述错误。
【解决方案2】:

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

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-06-25
    • 2017-12-19
    • 1970-01-01
    相关资源
    最近更新 更多