Home > database >  is there an better way to handle routing of urls in a console app C#
is there an better way to handle routing of urls in a console app C#

Time:11-24

Here is my code example, stripped a majority of the URLs and code inside them to make it easier to read https://pastebin.com/DBQrjJ8F

Is there a better way to handle the routing of the URLs, to trigger the respective functions for each URL?

I've tried the code example above and Splitting the Absolute URL by / but I was not sure where to go from there. Any code examples or explanations etc would be greatly appreciated

I don't write c# a lot so please forgive me if I'm missing something obvious

if((inputRequest.HttpMethod == "POST") && (inputRequest.Url.AbsolutePath == "/API/Auth"))
{
    Auth();
}
else if((inputRequest.HttpMethod == "GET") && (inputRequest.Url.AbsolutePath == "/API/Shutdown"))
{
    Shutdown();
}
else if((inputRequest.HttpMethod == "POST") && (inputRequest.Url.AbsolutePath == "/API/Script/Exec"))
{
    ScriptExec();
}
else
{
    SendBackUnknownURLMessage();
}

CodePudding user response:

you are doing Api Gateway by yourself. better way is to use Api Gateway nuget packages like 'Ocelot', that handles this issue by itself.

https://ocelot.readthedocs.io/en/latest/index.html

CodePudding user response:

I decided to break the URL into segments like /API/ClassName/MethodName

I would then check if the ClassName exists. If it does, then check if the method exists in that class. if so, create an instance of the class and Invoke the method.

and pass along any additional arguments and headers to it for parsing later on.

  • Related