Reputation: 717
I have some angular attempting to post to a web API. I have got this working before but keep getting issues with CORS.
I have put this in global.asax
protected void Application_BeginRequest(object sender, EventArgs e)
{
HttpContext.Current.Response.AddHeader("Access-Control-Allow-Origin", "*");
if (HttpContext.Current.Request.HttpMethod == "OPTIONS")
{
HttpContext.Current.Response.AddHeader("Cache-Control", "no-cache");
HttpContext.Current.Response.AddHeader("Access-Control-Allow-Methods", "GET, POST, DELETE, PUT");
HttpContext.Current.Response.AddHeader("Access-Control-Allow-Headers", "Content-Type, Accept");
HttpContext.Current.Response.AddHeader("Access-Control-Max-Age", "1728000");
HttpContext.Current.Response.End();
}
}
this is the API Controller action signature:
public void Post([FromBody]Message message)
both [FromUri] and [FromBody] do not work
I have tried:
config.EnableCors(new EnableCorsAttribute("*", "*", "*"));
in the WebApiConfig.cs while removing Application_BeginRequest code in asax file and I get the same message which is: 405 (Method Not Allowed)
And the Controller:
public class MessagingController : ApiController
{
private IMessagingRepository DataRepository;
public MessagingController()
{
DataRepository = new DataRepositoryFactory().GetMessagingInstance();
}
public List<Message> Get([FromUri]MessageGetModel model)
{
var result = DataRepository.GetMessages(model.FromId, model.ToId);
return result;
}
public Message Get(string messageId)
{
var result = DataRepository.GetMessage(messageId);
return result;
}
public void Post([FromUri]Message message)
{
message.Id = ObjectId.GenerateNewId().ToString();
DataRepository.GetDataRepository().Save(message);
DataRepository.PostOrUpdateThread(message);
}
}
Upvotes: 1
Views: 1304
Reputation: 4130
1- Change [FromUri] to [FromBody] because you normally post the data through the message body not through the query string, normally the query string is for Get requests.
2- Remove your code from Application_BeginRequest because EnableCors is enough, and it will add the Allow headers, and by adding them again through the code of Application_BeginRequest, you get problems of duplication for the same header in your response.
3- You can add the Route Attribute or RoutePrefix to your method/controller so for your Post method, replace it to be something like this:
[Route("api/Messaging")]
public void Post([FromBody]Message message)
{
message.Id = ObjectId.GenerateNewId().ToString();
DataRepository.GetDataRepository().Save(message);
DataRepository.PostOrUpdateThread(message);
}
4- Test with Fiddler or postman and make sure that you add your message into the request body either in a JSON format or url encoded.
5- Make sure that use set the "content-type" header to "application/json" if you set the message in the previous point to be in a JSON format, or set the content-type header to "application/x-www-form-urlencoded" if you encoded the message object in the previous point.
6- Finally, you need to make sure that your request url is posting to yourapilocatio/api/Messaging url.
Also, you can see my answer here.
Hope this helps.
Upvotes: 0