没有基于约定的路由的web api属性路由

时间:2015-11-30 21:10:57

标签: routing asp.net-web-api2 owin asp.net-web-api-routing attributerouting

我在一个解决方案中有web api(owin托管)和MVC项目。如果我有基于约定但属性路由不起作用,我的web api路由工作。为了测试属性路由,我删除了我的web api路由的默认的基于约定的配置。我有以下条目

startup.cs

public partial class Startup
 {

   public void Configuration(IAppBuilder app)
   { 
    var config = new HttpConfiguration();
     app.Map("/api", inner =>
        {
            ConfigureWebApi(config);
            inner.UseWebApi(config);
        });
   }

   public void ConfigureWebApi(HttpConfiguration config)
   {
        // Web API configuration and services

        // Web API routes
        config.MapHttpAttributeRoutes();
   } // no default convention based routing

ProductsController.cs

public class ProductsController : ApiController
 {
    [Route("api/products")]
    public IHttpActionResult Get()
    {

    }

     [Route("api/products/TopCategories"),HttpGet]
    public IHttpActionResult GetTopCategories()
    {

    }
 }

我不想依赖于web api的MVC路由,但是如果有任何遗漏,请告诉我。

routeconfig.cs

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

的Global.asax.cs

public class MvcApplication : System.Web.HttpApplication
{
    protected void Application_Start()
    {
        AreaRegistration.RegisterAllAreas();
        //web api routes from startup.cs
        //GlobalConfiguration.Configure(WebApiConfig.Register);
        FilterConfig.RegisterGlobalFilters(GlobalFilters.Filters);
        RouteConfig.RegisterRoutes(RouteTable.Routes);
        BundleConfig.RegisterBundles(BundleTable.Bundles);
    }
}

我使用了 routeprefix ,但已将其移除以进行测试。 我认为以下两个网址都应该有效,但由于某些原因它们不是,我收到404错误。

1)http://localhost:xxxx/api/products 2)http://localhost:xxxx/api/products/TopCategories

基于堆栈跟踪,出于某种原因,我认为我的MVC路由正在处理" / api"路线(我可能错了)。

   System.Web.HttpException (0x80004005): The controller for path    '/api/products' was not found or does not implement IController.
   at System.Web.Mvc.DefaultControllerFactory.GetControllerInstance(RequestContext   requestContext, Type controllerType)
   at System.Web.Mvc.DefaultControllerFactory.CreateController(RequestContext requestContext, String controllerName)
   at System.Web.Mvc.MvcHandler.ProcessRequestInit(HttpContextBase httpContext, IController& controller, IControllerFactory& factory)
   at System.Web.Mvc.MvcHandler.BeginProcessRequest(HttpContextBase httpContext, AsyncCallback callback, Object state)
   at System.Web.Mvc.MvcHandler.BeginProcessRequest(HttpContext httpContext, AsyncCallback callback, Object state)
   at System.Web.Mvc.MvcHandler.System.Web.IHttpAsyncHandler.BeginProcessRequest(HttpContext context, AsyncCallback cb, Object extraData)
   at System.Web.HttpApplication.CallHandlerExecutionStep.System.Web.HttpApplication.IExecutionStep.Execute()
   at System.Web.HttpApplication.ExecuteStep(IExecutionStep step, Boolean& completedSynchronously)

0 个答案:

没有答案