2
votes

I'm new to orchard cms. I've created a custom module with api controller. The only problem is I can't get the data from api when I use cross domain

2
I have the same problem. I don't know how to access HttpConfiguration. The ControllerContext.Configuration property is always null. Did u find a solution for this?Thx - Daniel Leiszen
The comment here from @DanielLeiszen is a good answer to this question. - Xceno

2 Answers

2
votes

I had added the below lines of code on Global.asax of Orchard.Web and it works

protected void Application_PreSendRequestHeaders(object sender, EventArgs e)
    {
        HttpContext.Current.Response.Headers.Remove("X-Frame-Options");
        HttpContext.Current.Response.Headers.Add("Access-Control-Allow-Origin", "*");
    }
1
votes

There is one trick that you don't need to break the original orchard source. You catch and response the preflight request with expected config.

public class ProductsController : ApiController
{
    [HttpOptions]
    public HttpResponseMessage EnableCors()
    {
        var response = Request.CreateResponse();
        response.Headers.Add("Access-Control-Allow-Origin", "your client");
        response.Headers.Add("Access-Control-Allow-Methods", "methods");
        response.Headers.Add("Access-Control-Allow-Headers", "content-type");
        return response;
    }

}