Setup
- Edit your web.config
<?xml version="1.0"?> <configuration> <appSettings> <add key="Nina.Path" value="~/api"/> <add key="Nina.Cors" value="*"/> </appSettings> <system.web> <compilation targetFramework="4.5" debug="true"/> <httpModules> <add name="Nina" type="Nina.Startup, Nina"/> </httpModules> </system.web> <system.webServer> <validation validateIntegratedModeConfiguration="false"/> <modules> <add name="Nina" type="Nina.Startup, Nina"/> </modules> </system.webServer> <location path="api" allowOverride="true"> <system.webServer> <handlers> <clear/> <add name="ExtensionlessUrl-Integrated-4.0" path="*" verb="GET,HEAD,POST,DEBUG,PUT,DELETE" type="System.Web.Handlers.TransferRequestHandler" preCondition="integratedMode,runtimeVersionv4.0" responseBufferLimit="0"/> </handlers> </system.webServer> </location> </configuration>
ATENTION: Always re-build after change your web.config
- Write your
apimodules (see below)
Modules
Modules class must inherit from NinaModule. With you want, decorate class with [BaseUrl] to set base url.
[BaseUrl("/users")] pubic class UserApi : NinaModule { [Get("/{id}")] public User GetUser(int id) { // GET /api/users/123 } [Post("/{id}")] public int Post(UserView user) { // PUT /api/users } [Get("/code/{id:(\\[a-z]{8})}")] public string GetCode(string id) { // GET /api/users/code/abcdefgh } [ValidateInput] // Validate request data for html tags [Put("/{id}")] public int Post(int id, NameValueCollection data, UserView user, JObejct user2, HttpFileCollection files, Stream input) { // PUT /api/users/123 } }
Model Binding
Parameters bind rules:
- If parameter name found on route pattern? Use route value (
/edit/{id}) - If parameter name not found on route:
- Parameter is
String? UseRequest.Form.ToString() - Parameter is
NameValueCollection? UseRequest.Params= Form + QueryString + Cookies + ServerVariables - Parameter is
Stream? UseRequest.InputStream - Parameter is
IPrincipal? UseHttpContext.User - Parameter is
HttpContext? UseHttpContext - Parameter is
HttpContextWrapper? Usenew HttpContextWrapper(context) - Parameter is
JObject? UseJObject.Parse() - Parameter any other type? Use
JsonDeserialize(model, parameterType) - Model is
Null? Usedefault(T)
- Parameter is
Result actions
JsonResult- Returns data as JSON string. Is default result when method returns a nonBaseResultErrorResult- Returns an HTTP error in JSON formatFileContentResult- Returns a file from a streamFileResult- Returns a file from diskJsonPResult- Returns JSON string with callback() functionTextResult- Returns HTML data
Authentication & authorization
- Use
[Authorize]attribute - checksHttpContext.User.Identity.IsAuthenticated - Use
[Role("admin", "user")]attribute - checkesHttpContext.User.IsInRole()
TODO
- Support for multiples MountPoints
- Permit path like
/download/{path*}