控制器
[Authorize]
public class EnumController : ApiController
{
[HttpGet]
public IEnumerable StatusVenda()
{
return Enum.GetValues(typeof(StatusVendaEnum)).Cast<object>()
.Select(v => new
{
Display = v.GetDisplayText(),
Id = v.ToString(),
Class = v.GetGroupText()
}).ToList();
}
}
配置
public static class WebApiConfig
{
public static void Register(HttpConfiguration config)
{
// OData
config.Routes.MapODataRoute(routeName: "OData", routePrefix: "odata", model: CreateModelBuider(config).GetEdmModel());
config.EnableQuerySupport();
// Need this because enum
// Tips from http://stackoverflow.com/questions/11407267/multiple-httppost-method-in-mvc4-web-api-controller
// Controller Only -> To handle routes like `/api/Usuarios`
config.Routes.MapHttpRoute(
name: "ControllerOnly",
routeTemplate: "Api/{controller}"
);
// Controller with ID -> To handle routes like `/api/pessoas/1`
config.Routes.MapHttpRoute(
name: "ControllerAndId",
routeTemplate: "Api/{controller}/{id}",
defaults: null,
constraints: new { id = @"^\d+$" } // Only integers
);
// Controllers with Actions -> To handle routes like `/api/pessoas/getProfissoes`
config.Routes.MapHttpRoute(
name: "ControllerAndAction",
routeTemplate: "Api/{controller}/{action}",
defaults: new { action = "Get" }
);
config.Routes.MapHttpRoute(
name: "ControllerActionAndId",
routeTemplate: "Api/{controller}/{action}/{id}",
defaults: null,
constraints: new { id = @"^\d+$" } // Only integers
);
#region Configurations
var jsonFormatter = GlobalConfiguration.Configuration.Formatters.JsonFormatter;
var enumConverter = new StringEnumConverter();
jsonFormatter.SerializerSettings.Converters.Add(enumConverter);
var jqueryFormatter = config.Formatters.FirstOrDefault(x => x.GetType() == typeof(JQueryMvcFormUrlEncodedFormatter));
config.Formatters.Remove(config.Formatters.XmlFormatter);
config.Formatters.Remove(config.Formatters.FormUrlEncodedFormatter);
config.Formatters.Remove(jqueryFormatter);
jsonFormatter.SerializerSettings.PreserveReferencesHandling = PreserveReferencesHandling.None;
jsonFormatter.SerializerSettings.ReferenceLoopHandling = ReferenceLoopHandling.Ignore;
jsonFormatter.SerializerSettings.DateFormatHandling = DateFormatHandling.IsoDateFormat;
jsonFormatter.SerializerSettings.DefaultValueHandling = DefaultValueHandling.Include;
config.Formatters.JsonFormatter.SerializerSettings.DefaultValueHandling = DefaultValueHandling.Include;
#endregion
}
}
的global.asax.cs
ViewEngines.Engines.Clear();
ViewEngines.Engines.Add(new RazorViewEngine() { FileExtensions = new string[] { "cshtml" } });
AreaRegistration.RegisterAllAreas();
ValidatorOptions.ResourceProviderType = typeof(Resources.FluentValidation);
SecurityConfigurator.Configure(config =>
{
config.DefaultPolicyViolationHandlerIs(() => new DefaultPolicyViolationHandler());
config.GetAuthenticationStatusFrom(() => WebSecurity.IsAuthenticated);
config.GetRolesFrom(() => Roles.GetRolesForUser());
config.ForAllControllers().RequireAnyRole(RolesConst.Administrador);
// Others config ommited
});
FilterConfig.RegisterGlobalFilters(GlobalFilters.Filters);
RouteConfig.RegisterRoutes(RouteTable.Routes);
WebApiConfig.Register(GlobalConfiguration.Configuration);
BundleConfig.RegisterBundles(BundleTable.Bundles, Server);
ModelBinders.Binders.DefaultBinder = new MyDefaultModelBinder();
LazyInitializer.EnsureInitialized(ref _initializer, ref _isInitialized, ref _initializerLock);
当我向/Api/Enum/StatusVenda
发出请求时,会返回404错误!
答案 0 :(得分:2)
“请注意,当您在web-api配置中注册odata路由时,必须将其放置在默认的全部路由之前”
来源:http://tech.trailmax.info/2014/02/odata-controllers-in-webapi-2-1-getting-404-page-not-found/