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

GWT and Open-ID using Spring Security

In this post I'll combine the GWT and Spring Security integration from http://technowobble.blogspot.com/2010/05/gwt-and-spring-security.html and the Open-ID using Spring Security from http://technowobble.blogspot.com/2010/06/using-spring-securitys-openid.html . I'm assuming you've read them before reading further... :) I was also inspired by http://www.sociallipstick.com/?p=86 and http://code.google.com/p/dyuproject/wiki/OpenidLoginWithoutLeavingPage to get this working with a pop-up as my sample application is based on GWT - hence, I don't want to direct the user to another page and loose the application state etc. I'm also showing how to exchange Open-ID attributes with e.g. Google. As with the previous blogposts, the sample application is runnable on Google App Engine. With no further ado, this is basically what is needed to add Open-ID support to my previous sample application: From my second post, add Openid4javaFetcher, MyHttpCacheProvider and OpenI
A quick note on something that's been bugging me while using SpringSource STS with the GWT-plugin on my MacBook Pro... Sometimes, when shutting down the internal devmode server I get an error dialog saying it wasn't able to shut down the process fully (even though it looks shut down in the console window). When starting it again, it reports port 9997 to be in use already - and consequently fails to start up again. I haven't found a way to find the process within SpringSource STS, even though there are probably several ways of doing it (which I do not know about). Anyhow - I solved it using the " lsof " package (List Open Files). To find the PID of the process using port 9997, type "lsof -i :9997" in a Terminal window. A simple "kill -9 &ltpid&gt" will take care of the rest. I'm not sure if this happens on other platforms etc, but at least, now you know how to handle it if it hits you!