Lau*_*son 1 asp.net-mvc asp.net-web-api asp.net-web-api-routing
当我尝试不带可选参数的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);
}
Run Code Online (Sandbox Code Playgroud)
在APIAreaRegistration.cs中:
public override void RegisterArea(AreaRegistrationContext context)
{
//context.MapRoute(
// "API_default",
// "API/{controller}/{action}/{id}",
// new { action = "Index", id = UrlParameter.Optional }
//);
}
Run Code Online (Sandbox Code Playgroud)
在Global.asax.cs中:
protected void Application_Start()
{
AreaRegistration.RegisterAllAreas();
GlobalConfiguration.Configure(WebApiConfig.Register);
//WebApiConfig.Register(GlobalConfiguration.Configuration);
WebApiConfig.RegisterSiteMap();
RouteConfig.RegisterRoutes(RouteTable.Routes);
}
Run Code Online (Sandbox Code Playgroud)
(请注意:我尝试使用注释行与其上方的行,没有区别)
这是我有问题的控制器:
[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;
}
Run Code Online (Sandbox Code Playgroud)
最后,这是我的查看代码:
@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" }))))
Run Code Online (Sandbox Code Playgroud)
如果我在action =语句后添加id = 0,它将不再返回404错误,而是继续返回0节点,而不是返回当前节点的子节点。如果我省略id = 0,它将返回404错误,即
/ API / LocationFilterAPI / GetLocations / 0-正常工作
/ API / LocationFilterAPI / GetLocations-返回404
我在此控制器中进行的所有其他API调用都具有必需参数或根本没有参数,并且所有这些都工作正常。我整个下午都在撞头。任何见识将不胜感激。
谢谢!
劳里
ps为了回应评论,下面是我的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" }
);
}
Run Code Online (Sandbox Code Playgroud)
尝试为您的方法参数分配null indicating that is optional
public IEnumerable<LocationFilterViewModel> GetLocations(int? id = null)
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
962 次 |
| 最近记录: |