DZone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Over 2 million developers have joined DZone.
Log In / Join
Please enter at least three characters to search
Refcards Trend Reports
Events Video Library
Refcards
Trend Reports

Events

View Events Video Library

Zones

Culture and Methodologies Agile Career Development Methodologies Team Management
Data Engineering AI/ML Big Data Data Databases IoT
Software Design and Architecture Cloud Architecture Containers Integration Microservices Performance Security
Coding Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Culture and Methodologies
Agile Career Development Methodologies Team Management
Data Engineering
AI/ML Big Data Data Databases IoT
Software Design and Architecture
Cloud Architecture Containers Integration Microservices Performance Security
Coding
Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance
Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks

The software you build is only as secure as the code that powers it. Learn how malicious code creeps into your software supply chain.

Apache Cassandra combines the benefits of major NoSQL databases to support data management needs not covered by traditional RDBMS vendors.

Generative AI has transformed nearly every industry. How can you leverage GenAI to improve your productivity and efficiency?

Modernize your data layer. Learn how to design cloud-native database architectures to meet the evolving demands of AI and GenAI workloads.

Related

  • Segmentation Violation and How Rust Helps Overcome It
  • Mastering Ownership and Borrowing in Rust
  • How I Taught OpenAI a New Programming Language With Fine-Tuning
  • Rust vs Python: Differences and Ideal Use Cases

Trending

  • Enforcing Architecture With ArchUnit in Java
  • How to Ensure Cross-Time Zone Data Integrity and Consistency in Global Data Pipelines
  • Accelerating Debugging in Integration Testing: An Efficient Search-Based Workflow for Impact Localization
  • Detection and Mitigation of Lateral Movement in Cloud Networks
  1. DZone
  2. Coding
  3. Languages
  4. Dynamically resolving URLs in .Net 5

Dynamically resolving URLs in .Net 5

Should you choose a static programming language or a dynamic runtime? And why is Winnie the Pooh relevant to the answer to that question?

By 
Thomas Hansen user avatar
Thomas Hansen
DZone Core CORE ·
Apr. 09, 21 · Tutorial
Likes (2)
Comment
Save
Tweet
Share
6.7K Views

Join the DZone community and get the full member experience.

Join For Free

My favourite feature with .Net and C# is that it's a static programming language. This is also the largest problem I have with .Net 5 - As in, its blessing also becomes its curse. Simply because sometime I need a bit more "dynamic nature" than that which .Net provides out of the box. However, .Net 5 has a lot of interesting features, allowing you to circumvent this, opening it up for a more "dynamic nature". Let me illustrate with an example C# HTTP API controller.

C#
 




x
9
10


 
1
[Route("magic")]
2
public class EndpointController : ControllerBase
3
{
4
    [HttpGet]
5
    [Route("{*url}")]
6
    public async Task<ActionResult> Get(string url)
7
    {
8
        /* Do interesting stuff with URL here */
9
    }
10
}


Notice the [Route("{*url}")] parts in the above code? This tiny little detail implies that everything going towards "magic/xxx" will be resolved by the controller, and its "xxx" parts will end up becoming the url argument to our method.

This tiny little trick allows you to create a dynamic controller, that resolves everything, within a root URL, where the URL itself becomes an argument to your method. This was what I hinted towards in my previous article when I said "now all you need to do is a way to dynamically invoke endpoints". You can see the entire code in my magic.endpoint project, specifically in its EndpointController class. The MagicEndpoint controller class in Magic, basically resolves all the 5 most commonly used verbs in this way.

  • GET
  • POST
  • PUT
  • DELETE
  • PATCH

All the methods in this controller, takes the URL dynamically, where the relative URL becomes an argument to its method. This allows me to dynamically load my Hyperlambda files, and execute them as such. All in all quite brilliant if I may be so courageous as to say so myself in fact :)

Using tiny little tricks as this, I am able to take something that is fundamentally static in nature, turning it completely on its head, making it 100% dynamic. This allows me from a use case basis to choose which paradigm I want to use for each individual problem I face. If I need static typing, I use normal C#. If I need a more dynamic runtime, I choose Hyperlambda. And both paradigms perfectly aligns with each other, allowing me to interoperate between everything with a simple signal invocation.

  • 100% dynamic platform - Check!
  • 100% static typing - Check!

It's almost as if you can hear Winnie the Pooh in the background answering "Yes please both" to the question of whether or not he wants chocolate or honey. Basically, once confronted with the question of "do you want static typing or a dynamic runtime", your knee jerk reaction should be as follows ...

Yes please, both!

The above little trick, allows you to use .Net API controller endpoints to literally execute any dynamic scripting language you need, where the URLs resolves to the script files on your server's file system. Of course, Magic only executes Hyperlambda files, but you can just as easily execute Lizzie files, Python files, or Ruby files for that matter - As long as it is an interpreted language of some sort. In fact, I am not sure if I would recommend it, but you could probably also resolve your HTTP invocations directly to SQL files using this little trick.

Below you can find a link to download Magic and try out the whole platform tying all of these constructs together if you wish.

  • Download Magic
C Language .NET programming langauge

Opinions expressed by DZone contributors are their own.

Related

  • Segmentation Violation and How Rust Helps Overcome It
  • Mastering Ownership and Borrowing in Rust
  • How I Taught OpenAI a New Programming Language With Fine-Tuning
  • Rust vs Python: Differences and Ideal Use Cases

Partner Resources

×

Comments
Oops! Something Went Wrong

The likes didn't load as expected. Please refresh the page and try again.

ABOUT US

  • About DZone
  • Support and feedback
  • Community research
  • Sitemap

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • Become a Contributor
  • Core Program
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 3343 Perimeter Hill Drive
  • Suite 100
  • Nashville, TN 37211
  • support@dzone.com

Let's be friends:

Likes
There are no likes...yet! 👀
Be the first to like this post!
It looks like you're not logged in.
Sign in to see who liked this post!