当我尝试在没有可选参数的情况下调用我的WebAPI控制器时,我遇到了404错误。我试过在Global.asax.cs中重新排序路由初始化命令无济于事。 WebAPI位于某个区域,我注释掉该区域的路由信息,因此找不到该路由。这就是我所拥有的:
在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" }))))
如果我在action =语句后添加id = 0,它不再返回404错误,但它不会返回当前节点的子节点,而是继续返回0节点。如果省略id = 0,则返回404错误,即
/ API / LocationFilterAPI / GetLocations / 0 - 工作正常
/ API / LocationFilterAPI / GetLocations - 返回404
我在此控制器中的每个其他API调用都有一个必需参数或根本没有参数,所有这些都正常工作。我整个下午都在反对这个问题。任何见解将不胜感激。
谢谢!
劳里
P.S。要回复以下评论,这是我的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 :(得分:4)
尝试为方法参数indicating that is optional
public IEnumerable<LocationFilterViewModel> GetLocations(int? id = null)