2015-04-30 49 views
1

我遇到404错误,当我尝试调用我的WebAPI控制器没有可选参数。我尝试在Global.asax.cs中重新排序路由初始化命令无济于事。 WebAPI位于一个区域内,我将该区域的路由信息​​注释掉,以便它找不到该路由。下面是我有:WebAPI路由与区域 - 404错误时丢失可选参数

在WebApiConfig.cs:

 public static void Register(HttpConfiguration configuration) 
    { 
     configuration.Routes.MapHttpRoute("DefaultAPI", 
      "API/{controller}/{action}/{id}", 
      new { id = RouteParameter.Optional }); 

     // From http://weblogs.asp.net/fbouma/how-to-make-asp-net-webapi-serialize-your-llblgen-pro-entities-to-json 
     var json = configuration.Formatters.JsonFormatter; 
     json.SerializerSettings.PreserveReferencesHandling = Newtonsoft.Json.PreserveReferencesHandling.Objects; 
     json.SerializerSettings.ContractResolver = new DefaultContractResolver() 
     { 
      IgnoreSerializableInterface = true, 
      IgnoreSerializableAttribute = true 
     }; 

     var appXmlType = configuration.Formatters.XmlFormatter.SupportedMediaTypes.FirstOrDefault(t => t.MediaType == "application/xml"); 
     configuration.Formatters.XmlFormatter.SupportedMediaTypes.Remove(appXmlType); 
    } 

在APIAreaRegistration.cs

 public override void RegisterArea(AreaRegistrationContext context) 
    { 
     //context.MapRoute(
     // "API_default", 
     // "API/{controller}/{action}/{id}", 
     // new { action = "Index", id = UrlParameter.Optional } 
     //); 
    } 

在Global.asax.cs中:

 protected void Application_Start() 
    { 
     AreaRegistration.RegisterAllAreas(); 
     GlobalConfiguration.Configure(WebApiConfig.Register); 
     //WebApiConfig.Register(GlobalConfiguration.Configuration); 
     WebApiConfig.RegisterSiteMap(); 

     RouteConfig.RegisterRoutes(RouteTable.Routes); 
    } 

(请注意:我使用注释掉的行与一个它上面并没有什么区别试过)

这里是我的问题的控制器:

 [HttpGet] 
    public IEnumerable<LocationFilterViewModel> GetLocations(int? id) 
    { 
     LocationFilterCollection Locations = LocationFilterCollection.GetLocations(id.HasValue ? id.Value : 0); 
     List<LocationFilterViewModel> myList = new List<LocationFilterViewModel>(); 
     foreach (LocationFilterEntity myEntity in Locations) 
     { 
      LocationFilterViewModel myModel = new LocationFilterViewModel(); 
      InitializeModel(myEntity, myModel); 
      myList.Add(myModel); 
     } 
     return myList; 
    } 

最后,在这里在我看来代码:

@Html.Kendo().TreeView().Name("Locations").LoadOnDemand(false).DataTextField("LocationName").DragAndDrop(false).Checkboxes(true).AutoBind(true).ExpandAll(true).DataSource(dataSource => dataSource 
.Model(model => model 
.Id("LocationId") 
.HasChildren("HasChildren")) 
.Read(read => read.Url(@Url.HttpRouteUrl("DefaultAPI", new { controller="LocationFilterAPI", action="GetLocations" })))) 

如果我添加ID = 0后的行动=语句,它不再返回404错误,但而不是返回当前节点的孩子,我t一直返回0节点。如果我省略了ID = 0,则返回404错误,即

/API/LocationFilterAPI /的getLocations/0 - 正常工作

/API/LocationFilterAPI /的getLocations - 返回404

每隔API调用我在这个控制器中有一个必需的参数或根本没有参数,并且所有这些工作都很好。整个下午我一直在抨击这件事。任何有识之士将不胜感激。

谢谢!

劳瑞

附:为了回应发表评论,下面,这里是我的RegisterRoutes方法:

 public static void RegisterRoutes(RouteCollection routes) 
    { 
     routes.IgnoreRoute("{resource}.axd/{*pathInfo}"); 

     routes.MapRoute(
      name: "LibraryCategoryList", 
      url: "Library/List/{id}/{id2}", 
      defaults: new { controller = "Library", action = "List", id = UrlParameter.Optional,id2=UrlParameter.Optional } 
      ); 
     routes.MapRoute(
      name: "Default", 
      url: "{controller}/{action}/{id}", 
      defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional }, 
      namespaces: new[] { "BFRDP.Controllers" } 
     ); 
    } 
+0

我猜不会对最终的参数,它是治疗'GetLocations'作为其他路由的参数。你的'Application_Start'也注册了MVC路由 - 你只显示了API路由。其他路线是什么样的? –

回答

4

尝试分配零到你的方法参数indicating that is optional

public IEnumerable<LocationFilterViewModel> GetLocations(int? id = null) 
+1

是的,工作!非常感谢! –