【问题标题】:Specify default controller/action route in WebAPI using AttributeRouting使用 AttributeRouting 在 WebAPI 中指定默认控制器/操作路由
【发布时间】:2013-04-22 08:27:18
【问题描述】:

当使用 AttributeRouting 而不是 WebAPI 使用的默认 RouteConfiguration 时,如何设置要使用的默认控制器。 即去掉注释代码部分,因为这在使用 AttribteRouting 时是多余的

    public class RouteConfig
    {
       public static void RegisterRoutes(RouteCollection routes)
       {
        routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
        //routes.MapRoute(
        //    name: "Default",
        //    url: "{controller}/{action}/{id}",
        //    defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional }
        //);

       }
     }

如果我评论上面的部分并尝试运行 webapi 应用程序,我会收到以下错误,因为没有定义默认的 Home 控制器/操作。 HTTP 错误 403.14 - 禁止 Web 服务器配置为不列出此目录的内容。

如何通过属性路由为 Home 控制器/动作指定路由?

编辑:代码示例:

 public class HomeController : Controller
{
    [GET("")]
    public ActionResult Index()
    {
        return View();
    }

    public ActionResult Help()
    {
        var explorer = GlobalConfiguration.Configuration.Services.GetApiExplorer();
        return View(new ApiModel(explorer));
    }
}

【问题讨论】:

  • 既然您写道您正在使用 AttributeRouting,请您在问题中提供一个代码示例,显示一个标有 AttributeRouting 属性的控制器操作?
  • @Pete:我已经更新了帖子以包含代码示例

标签: c# asp.net-web-api attributerouting


【解决方案1】:

如果您从 Create New Asp.net Web 应用程序模板中选中 MVC 和 Webapi,则标准 MVC 应用程序可以同时支持 MVC 和 Webapi。

您需要在 RouteConfig.cs 下执行以下操作,因为您的查询特定于 MVC 路由而不是 webapi 路由:

public class RouteConfig
{
   public static void RegisterRoutes(RouteCollection routes)
   {
    routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
    routes.MapMvcAttributeRoutes();
    routes.MapRoute(
        name: "Default",
        url: "{controller}/{action}/{id}",
        defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional });

   }
 }

注意带有“routes.MapMvcAttributeRoutes();”的行它提供属性路由,而对“routes.MapRoute(...)”的调用应该为您提供到默认控制器和操作的常规路由。

这是因为属性路由和常规路由可以混合使用。 希望对您有所帮助。

【讨论】:

    【解决方案2】:

    通过放置以下代码,在位于 App_Start 文件夹中的 WebApiConfig 类中启用属性路由:

    using System.Web.Http;
    
    namespace MarwakoService
    {
          public static class WebApiConfig
          {
                public static void Register(HttpConfiguration config)
                {
                    // Web API routes
                    config.MapHttpAttributeRoutes();
    
                   // Other Web API configuration not shown.
                }
           }
     }
    

    您可以结合基于约定的属性:

    public static class WebApiConfig
    {
         public static void Register(HttpConfiguration config)
         {
              // Attribute routing.
              config.MapHttpAttributeRoutes();
    
              // Convention-based routing.
              config.Routes.MapHttpRoute(
                  name: "DefaultApi",
                  routeTemplate: "api/{controller}/{id}",
                  defaults: new { id = RouteParameter.Optional }
                  );
         }
    }
    

    现在转到您的 global.asax 类并添加以下代码行:

    GlobalConfiguration.Configure(WebApiConfig.Register);
    

    您的 HomeController 是一个 MVC 控制器。尝试创建一个 API 控制器(假设是 CustomersController)并添加您的路由属性,如下所示:

    [RoutePrefix("api/Product")]
    public class CustomersController : ApiController
    {
        [Route("{customerName}/{customerID:int}/GetCreditCustomer")]
        public IEnumerable<uspSelectCreditCustomer> GetCreditCustomer(string customerName, int customerID)
        {
            ...
        }
    

    希望对你有帮助

    【讨论】:

      【解决方案3】:

      这对我有用。将此添加到WebApiConfig 类中的Register()

      config.Routes.MapHttpRoute(
                      name: "AppLaunch",
                      routeTemplate: "",
                      defaults: new
                      {
                          controller = "Home",
                          action = "Get"
                      }
                 );
      

      【讨论】:

        【解决方案4】:

        您需要在已安装的AttributeRouting (ASP.NET Web API) 旁边安装AttributeRouting (ASP.NET MVC)

        MVC 和 Web API 包是免费包。两者都依赖于 AttributeRouting.Core.* 包。 MVC 的 AR 用于控制器方法上的路由; Web API 的 AR 用于 ApiController 方法上的路由。

        【讨论】:

        • 您是否建议同时安装 AttributeRouting for MVC 和 AttributeRouting for WebAPI?
        • 是的,你需要添加MVC包。它们是免费包,都依赖于 AttributeRouting.Core.* 包。 MVC 的 AR 用于控制器方法上的路由; Web API 的 AR 用于 ApiController 方法上的路由。 (我编辑了我的答案以明确表示)
        【解决方案5】:

        您是否尝试过以下方法:

        //[RoutePrefix("")]
        public class HomeController : Controller
        {
            [GET("")]
            public ActionResult Index()
            {
                return View();
            }
        }
        

        这将在集合中添加一个路由,其 url 模板为“”,控制器和操作的默认值分别为“Home”和“Index”。

        【讨论】:

        • 以上似乎不起作用。我已更新帖子以包含代码 sn-p
        • 你能不能检查一下你是否在调用属性路由的扩展MapAttributeRoutes
        • 在AttributeRoutingHttp静态类的RegisterRoutes方法中是
        猜你喜欢
        • 2012-08-14
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2012-03-09
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多