-
Notifications
You must be signed in to change notification settings - Fork 241
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Adding documentation for Web Api in Orchard #205
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,76 @@ | ||
|
||
# Web Api in Orchard | ||
Web Api is available in Orchard. You can implement a web api to fit your needs in a custom module. | ||
|
||
|
||
## Creating Api Controllers | ||
The process of creating an Api Controller in Orchard is very similar to how you would do so in a standard .NET Web Api application. Create your controller class and have it inherit from ApiController: | ||
|
||
namespace MyCustomModule.Controllers.Api{ | ||
public class MyApiController : ApiController{ | ||
public IHttpActionResult Get(){ | ||
var itemsList = new List<string>{ | ||
"Item 1", | ||
"Item 2", | ||
"Item 3" | ||
}; | ||
|
||
return Ok(itemsList); | ||
} | ||
} | ||
} | ||
|
||
The above code sample will return the 3 item list shown in code when you hit the endpoint "/api/MyCustomModule/MyApi". | ||
|
||
## Declaring custom Api Routes | ||
|
||
To generate more friendly Api routes, you follow a similar process to declaring custom MVC routes in Orchard. Implement the IHttpRouteProvider interface like so: | ||
|
||
namespace MyCustomModule { | ||
public class ApiRoutes : IHttpRouteProvider { | ||
public IEnumerable<RouteDescriptor> GetRoutes() { | ||
return new RouteDescriptor[] { | ||
new HttpRouteDescriptor { | ||
Name = "Default Api", | ||
Priority = 0, | ||
RouteTemplate = "api/myapi/{id}", | ||
Defaults = new { | ||
area = "MyCustomModule", | ||
controller = "MyApi", | ||
id = RouteParameter.Optional | ||
} | ||
} | ||
}; | ||
} | ||
|
||
public void GetRoutes(ICollection<RouteDescriptor> routes) { | ||
foreach (RouteDescriptor routeDescriptor in GetRoutes()) { | ||
routes.Add(routeDescriptor); | ||
} | ||
} | ||
} | ||
} | ||
|
||
Now, the Api endpoint can be reached by hitting "/api/myapi". | ||
|
||
## Configuring Web Api in Orchard | ||
|
||
Since Orchard doesn't have the concept of an AppStart file, in order to add custom configuration to Web Api in Orchard, you must do so in an Autofac module. For example, the following will set the default Web Api return type to Json, and will ensure that Json objects/properties returned by the Api follow the camelCased naming convention. | ||
|
||
namespace Blue.Core { | ||
public class WebApiConfig : Module { | ||
protected override void Load(ContainerBuilder builder) { | ||
GlobalConfiguration.Configuration.Formatters.JsonFormatter.SupportedMediaTypes.Add(new MediaTypeHeaderValue("text/html")); | ||
|
||
var jsonFormatter = GlobalConfiguration.Configuration.Formatters.OfType<JsonMediaTypeFormatter>().FirstOrDefault(); | ||
|
||
if (jsonFormatter != null) { | ||
jsonFormatter.SerializerSettings.ContractResolver = new CamelCasePropertyNamesContractResolver(); | ||
} | ||
} | ||
} | ||
} | ||
|
||
## Conclusion | ||
|
||
This document should provide the basics of getting started with Web Api in Orchard. Enjoy! |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
You should have kept the same namespace than your example : MyCustomModule