ASP.NET MVC (via Core or older): Easy controller routing
-
Background - Escape From Legacy Java We have a 20 yr old Java windows service and we are trying to replace it (in production env.) with a C# service. JNI (Java Native Interface) Ridiculous! You have to write a C wrapper to every C# method. Nope. The tooling itself is like a cheese-grater down your face. Has to be an easier way. There is! Write C# Methods : Place Them In WebAPI Here it is. Just write up a nice C# ASP.NET MVC app. 1. Write each (Java) replacement method in C# 2. Provide a Controller\Action route to each method 3. Call each one as needed from Java This will help us prove out that the C# code handles everything the same way as the Java. Now The Weird/Wonderful Attempting to fire up the ASP.NET MVC Controller which simply provides the C# version of each Java method. Routing Fail! I spin it up and try to do a simple get on the method. Fail! Routing is weird or something? What? What!?! Hack around. Search. Hack some more. "Whyn't it hitting the controller?" :confused: Here's the Weird/Wonderful I Promised In the Controller just add this little decorator to the Action (C# method) and it's available via Get.
[HttpGet("GetTime")]
public string GetTime()
{
return DateTime.Now.ToString("HH:mm:ss");
}Now you can hit :
http://localhost:5231/MyController/GetTime
Easy-squeezy lemon peezy. Docs Though? Try finding the documentation on that for me. I found something similar but not quite this. At first I had :[HttpGet(Name="GetTime")]
That would compile but doesn't work. It's not the Name value you have to pass in, it's the Template value. :confused: I shall be liberated from Java. :rolleyes: