I have an ASP.NET website that contains a number of handlers (.ashx). Is it possible to call that handler from a standalone C# REST API (Web API)? Or does it need to be in the same solution file?
For example, my handler file contains the following code
public override void ProcessRequest(HttpContext context)
{
base.ProcessRequest(context);
context.Response.ContentType = "text/xml;charset=utf-8";
try
{
string action = context.Request["action"] ?? "";
switch (action.ToUpper())
{
case GETDATA:
GetDataActions(context);
break;
case SETDATA:
SetDataActions(context);
break;
default:
throw new Exception("");
}
}
catch (Exception genericException)
{
context.Response.StatusCode = 500;
WriteResponse(context, xError.ToString());
}
}
I would like to call SETDATA action from my WEB API controller.
Can anyone guide me?
3
Answers
You can reuse the handlers by calling them like a WebApi.
Make sure you have configured the handlers in your .config file and define a routing for them.
Find more info here
https://learn.microsoft.com/en-us/troubleshoot/developer/webapps/aspnet/development/http-modules-handlers
You can use
WebClient
to call your ashx files such as:You can use like below:
Detail link from microsoft: Calling a web api