Resolve the "Method not allowed" issue while performing Dynamic connection string through External API in JavaScript Embedding
When working with dynamic connection strings through an external API, you may encounter a “Method Not Allowed” error in JavaScript embedding. This issue often arises in the embedded dashboard, even though the external API returns a value while running the external API application.
Solution
The issue can be resolved by adding the HTTPHead
attribute to the method. This attribute indicates that the method will respond to HTTP HEAD requests. The HTTP HEAD method requests the headers that are returned if the specified resource would be requested with an HTTP GET method. Such a request can be done before deciding to download a large resource to save bandwidth, for example.
Here is a sample code snippet that demonstrates how to add the HTTPHead attribute to the method:
[HttpHead, HttpGet]
[Route("api/get-connection-details")]
public ApiResponse GetConnectionString()
{
try
{
var dataSourceName = HttpContext.Current.Request.Params["datasourcename"];
.......
}
catch (Exception ex)
{
..........
}
}
In the above code, the [HttpHead, HttpGet]
attributes are added before the GetConnectionString()
method. This allows the method to respond to both HTTP HEAD and GET requests.