WebAPI route file location

Topics: Administration, Customizing Orchard, General, Writing modules
Dec 3, 2014 at 11:38 PM
What is the best practice for a file location for WebAPI routes in orchard?


Just wondering what you guys do with it
Dec 4, 2014 at 12:43 AM
it could be anywhere. for me, i would rather put it on the root of my custom module.
Dec 10, 2014 at 1:19 AM
You never, ever touch global.asax in an Orchard application: it's a global resource. Fortunately, you don't need to. Check out this post: https://weblogs.asp.net/bleroy/building-a-webapi-route-in-orchard
Dec 10, 2014 at 11:54 AM
Thanks for your response, I have been very regular on your blog and am grateful for you posting that information. My question, which wasn't covered in your blog post was about best practice about where you put your routes. Clearly global.asax is a bad idea. I do need to add routes as part of getting web api urls, check out this post :) https://weblogs.asp.net/bleroy/cleanly-getting-a-webapi-action-url
Dec 15, 2014 at 8:04 PM
I'm a little confused. You define your routes from your module, in a class that implements IHttpRouteProvider. I'm pretty sure that was the whole point of the post ;) Did I misunderstand your question?
Dec 15, 2014 at 8:12 PM
Edited Dec 15, 2014 at 8:12 PM
My main question here was about where to put that class, one person suggested the root of the custom module, which sounds reasonable to me. It also doesn't seem to matter the name of the file, correct?
Dec 15, 2014 at 8:13 PM
Oh, ok, well, personal preference here, but I try to avoid putting anything at the root of all but the simplest modules. I create a Routes folder where I put my route classes.
Dec 15, 2014 at 8:21 PM
That is the type of thing I was looking for, thanks Bertrand!
Jan 6, 2015 at 9:28 PM
If you advise not to touch the global.asax, where do we configure things such as JSON serialization and camel casing, custom filters etc.? Below is my common WebApiConfig.cs. Where does that go in Orchard?
            // Web API configuration and services
            // Configure Web API to use only bearer token authentication.
            config.Filters.Add(new HostAuthenticationFilter(OAuthDefaults.AuthenticationType));
            //config.Filters.Add(new DbFailureExceptionFilterAttribute());

            // Use camel case for JSON data.
            config.Formatters.JsonFormatter.SerializerSettings.ContractResolver = new CamelCasePropertyNamesContractResolver();

            // default to JSON formatter for API responses
            config.Formatters.JsonFormatter.SupportedMediaTypes.Add(new MediaTypeHeaderValue("text/html"));

            // validation of input DTOs
            config.Filters.Add(new ValidateModelStateAttribute());
            // exception handling for actions
            config.Filters.Add(new LoggingExceptionsAttribute());
            // metadata to responses
            config.Filters.Add(new MetadataDecoratorAttribute());

Jan 8, 2015 at 12:40 PM
For anyone else wondering how to set serialization settings in Orchard Web APIs I did it within the API controller by setting and using the formatter when returning the result:
var formatter = new JsonMediaTypeFormatter();
  var json =formatter.SerializerSettings;

  json.DateFormatHandling = Newtonsoft.Json.DateFormatHandling.MicrosoftDateFormat;
  json.DateTimeZoneHandling = Newtonsoft.Json.DateTimeZoneHandling.Utc;
  json.NullValueHandling = Newtonsoft.Json.NullValueHandling.Ignore;
  json.Formatting = Newtonsoft.Json.Formatting.Indented;
  json.ContractResolver = new CamelCasePropertyNamesContractResolver();
  json.Culture = new CultureInfo("it-IT");

  return Request.CreateResponse(HttpStatusCode.OK, result, formatter);
You could also extract it into a helper/utility method somewhere..