sebu sebu - 28 days ago 8
C# Question

Web API not calling, if parameter consist @ character

I have an API, which is structured by three params (param1,param2,param3). for email parameter its not working, because '@' character. but normally working fine. please check my below code.

API Controller

public class ApiPINVerificationController : ApiController
{
public ApiPINVerificationController()
{

}

[HttpPost]
public IHttpActionResult GetStudentByPIN(string param1,string param2,string param3)
{
//Balance Search Audit Log Entry
SLAF.DataAccessManager.DTO.BalanceSearchLog balanceSearchLog = new DataAccessManager.DTO.BalanceSearchLog
{
PIN=param1,
CellPhone=param2,
Email=param3
};
APIManager.InsertBalanceSearchLog(balanceSearchLog);
return Ok(new
{
Student=objStudent
});
}
}


WebApiConfig

public static class WebApiConfig
{
public static string UrlPrefixRelative { get { return "~/Api"; } }
public static void Register(HttpConfiguration config)
{
//config.Formatters.Add(new BsonMediaTypeFormatter());
config.Formatters.Remove(config.Formatters.XmlFormatter);

config.Routes.MapHttpRoute(
name: "DefaultApi",
routeTemplate: "api/{controller}/{action}/{param}",
defaults: new { param = RouteParameter.Optional }
);
config.Routes.MapHttpRoute(
name: "Api2",
routeTemplate: "api/{controller}/{action}/{param1}/{param2}"
);
config.Routes.MapHttpRoute(
name: "Api3",
routeTemplate: "api/{controller}/{action}/{param1}/{param2}/{param3}"
);
}
}

Answer Source

I'm pretty sure that the @ character isn't your problem. I see that the email is the last parameter and as it most likely ends in .com or something else, the URL doesn't work.

Try to add a trailing / character and you'll see that the API calls succeed.

https://youraddress.com/api/something/something/param1/param2/email@something.com/