http://itsnull.com/presentations/moveToCore/
Created by Kip Streithorst / @itsnull
"dependencies": { "Microsoft.AspNetCore.Mvc.WebApiCompatShim": "1.0.0", }
public void ConfigureServices(IServiceCollection services) {
//from this: services.AddMvc();
//to this:
services.AddMvc().AddWebApiConventions();
}
public void Configure(IApplicationBuilder app, IHostingEnvironment env, ILoggerFactory loggerFactory) {
//from this: app.UseMvc();
//to this:
app.UseMvc(options => options.MapWebApiRoute("DefaultApi", "api/{controller}/{id?}"));
}
public void ConfigureServices(IServiceCollection services) {
services.AddSingleton<ChoreRepository, ChoreRepository>();
}
"dependencies": { "Microsoft.AspNetCore.StaticFiles": "1.0.0", }
public void Configure(IApplicationBuilder app, IHostingEnvironment env, ILoggerFactory loggerFactory) {
app.UseStaticFiles();
}
public void Configure(IApplicationBuilder app, IHostingEnvironment env, ILoggerFactory loggerFactory) {
app.UseDefaultFiles();
//must be before app.UseStaticFiles
}
public void ConfigureServices(IServiceCollection services) {
//append AddJsonOptions to AddMvc() call
services.AddMvc().AddJsonOptions(opt => {
var res = opt.SerializerSettings.ContractResolver as DefaultContractResolver;
if (res != null) {
res.NamingStrategy = null;
}
});
}
//BODY:
$.ajax({
type: 'POST',
url: '/someUrl',
contentType: 'application/json',
data: JSON.stringify({ Name: 'Jane', Age: 2 })
});
//FORM:
$.ajax({
type: 'POST',
url: '/someUrl',
data: { Name: 'Jane', Age: 2 }
});
public override void OnException(ExceptionContext context) {
var statusCode = HttpStatusCode.OK;
if (context.Exception is DataConflictException) {
statusCode = HttpStatusCode.Conflict;
} else if (context.Exception is DataMissingException) {
statusCode = HttpStatusCode.NotFound;
} else if (context.Exception is InvalidRequestException) {
statusCode = HttpStatusCode.BadRequest;
} else if (context.Exception is TimeoutException) {
statusCode = HttpStatusCode.RequestTimeout;
}
if (statusCode != HttpStatusCode.OK) {
context.ExceptionHandled = true;
context.Result = new StatusCodeResult((int)statusCode);
}
}
public void ConfigureServices(IServiceCollection services) {
services.AddMvc(opt => {
opt.Filters.Add(new DataExceptionFilterAttribute());
});
}