Azure

Routing In Azure Perform

What’s Routing ?

 

Routing is the method of directing an HTTP request to a controller which has the mandatory enterprise required to course of the request and provides an applicable response. Utilizing routing, we outline the foundations on the mapping between URLs to their respective controllers and actions. Within the context of this text, It’s the operate route which can be utilized to ship a http request to our Azure operate.

 

Default operate route in Azure capabilities

 

By default, the route to your azure operate can be “localhost:7071/api/{functionName}”

  1. utilizing System;  
  2. utilizing System.IO;  
  3. utilizing System.Threading.Duties;  
  4. utilizing Microsoft.AspNetCore.Mvc;  
  5. utilizing Microsoft.Azure.WebJobs;  
  6. utilizing Microsoft.Azure.WebJobs.Extensions.Http;  
  7. utilizing Microsoft.AspNetCore.Http;  
  8. utilizing Microsoft.Extensions.Logging;  
  9. utilizing Newtonsoft.Json;  
  10.   
  11. namespace CustomRouting  
  12. {  
  13.     public static class Function1  
  14.     {  
  15.         [FunctionName(“Function1”)]  
  16.         public static async Activity<IActionResult> Run(  
  17.             [HttpTrigger(AuthorizationLevel.Function, “get”“post”, Route = null)] HttpRequest req,  
  18.             ILogger log)  
  19.         {  
  20.             log.LogInformation(“C# HTTP set off operate processed a request.”);  
  21.   
  22.             string title = req.Question[“name”];  
  23.   
  24.             string requestBody = await new StreamReader(req.Physique).ReadToEndAsync();  
  25.             dynamic knowledge = JsonConvert.DeserializeObject(requestBody);  
  26.             title = title ?? knowledge?.title;  
  27.   
  28.             string responseMessage = string.IsNullOrEmpty(title)  
  29.                 ? “This HTTP triggered operate executed efficiently. Move a title in the question string or in the request physique for a personalised response.”  
  30.                 : $“Howdy, {title}. This HTTP triggered operate executed efficiently.”;  
  31.   
  32.             return new OkObjectResult(responseMessage);  
  33.         }  
  34.     }  
  35. }  

Within the above snippet, the operate route is http://localhost:7071/api/Function1

 

Customized Routing utilizing the Route Parameter by modifying the route parameter within the HttpTriggerAttribute

 

We will replace or override the route for our Http Triggered Azure operate by modifying the route parameter within the function1.cs. By default the route parameter is assigned as null.

 

Suppose we would like the path to ship a request to our Azure operate as localhost:7071/api/hi there then we would want to easily assign the route parameter as hi there.

  1. public static class Function1  
  2.     {  
  3.         [FunctionName(“Function1”)]  
  4.         public static async Activity<IActionResult> Run(  
  5.             [HttpTrigger(AuthorizationLevel.Function, “get”“post”, Route = “Hello”)] HttpRequest req,  
  6.             ILogger log)  
  7.         {  
  8.             log.LogInformation(“C# HTTP set off operate processed a request.”);  
  9.   
  10.             string title = req.Question[“name”];  
  11.   
  12.             string requestBody = await new StreamReader(req.Physique).ReadToEndAsync();  
  13.             dynamic knowledge = JsonConvert.DeserializeObject(requestBody);  
  14.             title = title ?? knowledge?.title;  
  15.   
  16.             string responseMessage = string.IsNullOrEmpty(title)  
  17.                 ? “This HTTP triggered operate executed efficiently. Move a title in the question string or in the request physique for a personalised response.”  
  18.                 : $“Howdy, {title}. This HTTP triggered operate executed efficiently.”;  
  19.   
  20.             return new OkObjectResult(responseMessage);  
  21.         }  
  22.     }  

Now upon updating the route parameter, the brand new URL is http://localhost:7071/api/Howdy

 

Passing worth utilizing operate route in Azure operate

 

We all know that parameters or values might be handed with a url as a substitute of utilizing question string or request physique in net functions and Net APIs. We frequently use this whereas we’re sending a GET request to fetch knowledge for a specific report.

 

Instance – http://localhost:7071/api/Howdy/1

 

To cross worth within the operate route in Azure operate, we must modify the route parameter as “Howdy/{valueName}”. Then add a parameter with the identical title because the valueName within the Run technique to make use of this worth in your azure operate. However including {valueName} makes it a compulsory worth to be handed.

  1. public static class Function1  
  2.     {  
  3.         [FunctionName(“Function1”)]  
  4.         public static async Activity<IActionResult> Run(  
  5.             [HttpTrigger(AuthorizationLevel.Function, “get”“post”, Route = “Hello/{id}”)] HttpRequest req,  
  6.             string id,  
  7.             ILogger log)  
  8.         {  
  9.             log.LogInformation(“C# HTTP set off operate processed a request.”);  
  10.   
  11.             string title = req.Question[“name”];  
  12.   
  13.             string requestBody = await new StreamReader(req.Physique).ReadToEndAsync();  
  14.             dynamic knowledge = JsonConvert.DeserializeObject(requestBody);  
  15.             title = title ?? knowledge?.title;  
  16.   
  17.             string responseMessage = string.IsNullOrEmpty(title)  
  18.                 ? “This HTTP triggered operate executed efficiently. Move a title in the question string or in the request physique for a personalised response.”  
  19.                 : $“Howdy, {title}. This HTTP triggered operate executed efficiently.”;  
  20.   
  21.             return new OkObjectResult(responseMessage);  
  22.         }  
  23.     }  

Be aware

In case you do not cross a price within the operate route, the request will not go to the Azure operate. 

 

You may make it non-compulsory by including a ? Ultimately of valueName like {valueName?}. Confer with the under snippet to make it non-compulsory in operate route.

  1. public static class Function1  
  2.     {  
  3.         [FunctionName(“Function1”)]  
  4.         public static async Activity<IActionResult> Run(  
  5.             [HttpTrigger(AuthorizationLevel.Function, “get”“post”, Route = “Hello/{id?}”)] HttpRequest req,  
  6.             string id,  
  7.             ILogger log)  
  8.         {  
  9.             log.LogInformation(“C# HTTP set off operate processed a request.”);  
  10.   
  11.             string title = req.Question[“name”];  
  12.   
  13.             string requestBody = await new StreamReader(req.Physique).ReadToEndAsync();  
  14.             dynamic knowledge = JsonConvert.DeserializeObject(requestBody);  
  15.             title = title ?? knowledge?.title;  
  16.   
  17.             string responseMessage = string.IsNullOrEmpty(title)  
  18.                 ? “This HTTP triggered operate executed efficiently. Move a title in the question string or in the request physique for a personalised response.”  
  19.                 : $“Howdy, {title}. This HTTP triggered operate executed efficiently.”;  
  20.   
  21.             return new OkObjectResult(responseMessage);  
  22.         }  
  23.     }  

Now upon updating the route parameter, the brand new URL might be http://localhost:7071/api/Howdy/1 or http://localhost:7071/api/Howdy or http://localhost:7071/api/Howdy/ashirwad

 

Specifying the kind of the valueName to be accepted within the operate route.

 

By default, the kind of valueName is string however you may as well modify it to take worth of a selected kind by including “:valueType” after the parameter title within the curly braces.

 

Instance

 

To take an int kind worth within the operate route you’ll be able to specify the parameter as {valueName:int} and in case you intend to make it non-compulsory then you’ll be able to write it as {valueName:int?}.

 

You need to add a parameter within the run technique with the identical title and sort as that of the valueName to make use of it within the enterprise of your Azure operate.

  1. public static class Function1  
  2.     {  
  3.         [FunctionName(“Function1”)]  
  4.         public static async Activity<IActionResult> Run(  
  5.             [HttpTrigger(AuthorizationLevel.Function, “get”“post”, Route = “Hello/{id:int?}”)] HttpRequest req,  
  6.             int id,  
  7.             ILogger log)  
  8.         {  
  9.             log.LogInformation(“C# HTTP set off operate processed a request.”);  
  10.   
  11.             string title = req.Question[“name”];  
  12.   
  13.             string requestBody = await new StreamReader(req.Physique).ReadToEndAsync();  
  14.             dynamic knowledge = JsonConvert.DeserializeObject(requestBody);  
  15.             title = title ?? knowledge?.title;  
  16.   
  17.             string responseMessage = string.IsNullOrEmpty(title)  
  18.                 ? “This HTTP triggered operate executed efficiently. Move a title in the question string or in the request physique for a personalised response.”  
  19.                 : $“Howdy, {title}. This HTTP triggered operate executed efficiently.”;  
  20.   
  21.             return new OkObjectResult(responseMessage);  
  22.         }  
  23.     }  

Now upon updating the route parameter, the brand new URL might be http://localhost:7071/api/Howdy/1 or http://localhost:7071/api/Howdy 

 

Modifying the route prefix in Azure operate

 

You could have observed by now that the majority the operate routes had /api/ within the route. In case, you needed to alter it, you’ll be able to change it by modifying the content material utilizing the host.json.

 

Add the next snippet of line quantity 11 within the host.json to take away the route prefix within the operate route.

  1. {  
  2.     “model”“2.0”,  
  3.   “logging”: {  
  4.     “applicationInsights”: {  
  5.       “samplingExcludedTypes”“Request”,  
  6.       “samplingSettings”: {  
  7.         “isEnabled”true  
  8.       }  
  9.     }  
  10.   },  
  11.   “extensions”: {“http”: {“routePrefix”“”}}  
  12. }  

Now upon updating the route parameter, the brand new URL might be http://localhost:7071/Howdy/

 

In case you wish to have a route prefix then you’ll be able to add it contained in the quotes of routePrefix within the above snippet. 

 

Conclusion

 

Routing is sort of a necessary a part of creating an http primarily based resolution. Utilizing the route, the request would map to the Azure operate containing the enterprise logic to carry out the motion for that specific request. It’s fairly versatile in Azure to configure a route as per your necessities. The above article discusses all of the methods wherein routing might be modified for an Azure operate.

Show More

Related Articles

Leave a Reply

Your email address will not be published. Required fields are marked *

Back to top button