Skip to main content

Web API with ASP.NET Core MVC

I was exploring the new ASP.NET Core from Microsoft and being on a Mac, to start things off, I was following this guide, which includes setting up .NET Core and Visual Studio Code with the C# extension + scaffolding a template project using Yeoman. Pretty simple and works out of the box.

Now, to get going with Web API, the next guide would be https://docs.asp.net/en/latest/tutorials/first-web-api.html. However, this is based on Visual Studio 2015, where there are ready-made templates for a Web API project. I continued using the guide with my Yeoman template to see how far it would take me.

Turns out that I got pretty far, by just following the examples. The first thing that hit me was the change in the
ConfigureServices
file, namely the
services.AddMvc();
method. There was no notion of a MVC-framework in the generated Yeoman code, and I had to add
"Microsoft.AspNetCore.Mvc": "1.0.0"
to
project.json
to make it compile. To finally get the application to serve my API instead of the generated "Hello World" page, I changed
app.Run(async (context) =>
  {
    await context.Response.WriteAsync("Hello World!");
  });
to
app.UseMvcWithDefaultRoute();
To wrap things up, I changed the default namespace from
EmptyWeb1
to
TodoApi
to make things consistent.

At the very end of the guide, I also found the link to the source code, which could have saved me from figuring things out for myself.

/Mattias

Comments

Popular posts from this blog

GWT and Spring Security

Update! - Based on the post below, and my other post regarding Spring Security and OpenID, I have added Open-ID support to the sample application below. For those interested, here's the write-up of changes. I've spent quite some time digging into ways of integrating GWT and Spring Security. It all started by reading the following post in the GWT Forum - Best practices/ideas for GWT with Spring Security (or equivalent) , and then checking out this blog - GWT and Spring Security . To make matters worse, I started reading Security for GWT Applications and specifically about the "Cross-Site Request Forging"-attacks. Now, what could I do about it? Well, starting by setting up my own project (Maven-based) with all updated dependencies (GWT 2.0.3 etc) and started reading the Spring Security Reference Documentation (puh!). Instead of See Wah Cheng's approach of implementing a custom authentication service, I decided to rely on standard namespace configuration...

Using Spring Security's OpenID implementation (openid4java) on Google App Engine

The goal with this exercise is to have a running example of an OpenID login on a simple Spring application, using Google as the OpenID Provider. Note that the application will be running on Google App Engine and that Spring Roo is only used for simplicity of creating the project files. Any Spring-based application could use the same implementation. First of all, create a simple project using Spring Roo (or any equivalent framework), including the default security setup: project --topLevelPackage com.technowobble persistence setup --provider DATANUCLEUS --database GOOGLE_APP_ENGINE entity --class ~.domain.MyEntity field string --fieldName name controller all --package com.technowobble.controller security setup This setup only provides us with a form-login, which is not what we wanted. So what about OpenID? Well, if it wasn't for Google App Engine, I would happily have added an <openid-login>-tag to applicationContext-security.xml, but things are never that easy, are the...

Google Apps Script and ES Modules

Currently, Google Apps Script does not support ES modules - and any usage of export/import will fail. One way of handling this is to use rollup.js to bundle your project into one single JavaScript file. The trick here is to make sure not to export any functions in your entry point code, e.g. index.ts , and to prevent any generation of export statement in the final bundle (see the custom rollup plugin in the rollup.config.js below). import { babel } from "@rollup/plugin-babel"; import { nodeResolve } from "@rollup/plugin-node-resolve"; const extensions = [".ts", ".js"]; const preventThreeShakingPlugin = () => { return { name: 'no-threeshaking', resolveId(id, importer) { if (!importer) { // let's not theeshake entry points, as we're not exporting anything in Apps Script files return {id, moduleSideEffects: "no-treeshake" } } return null; }...