I'm using HttpSelfHostServer for my WebApi implementation. I need a Silverlight client to be able to access the API in a cross-domain manner. I've read the posts about adding jsonp and cors support for browser cross-domain access, and those work just fine.
However, silverlight doesn't work with these. Instead, it requires a ClientAccessPolicy.xml or a crossdomain.xml file to be created in the root of the web server. See
http://msdn.microsoft.com/en-us/library/cc197955(v=vs.95).aspx
If I was hosting on IIS, this wouldn't be an issue, but I can't figure out how to get the self-hosted http server to server up a static xml file. Is there a way to specify a route to a single static file? Or is there some way I can intercept the request
for these specific files and produce the xml it needs?
You could create a message handler to intercept the request and return the file. Read more about handlers here: http://www.asp.net/web-api/overview/working-with-http/http-message-handlers.
You will want to make sure to create the response, add your file stream, and return it. Just this alone is naive, but it should help you get started.
No need for any Message Handler. Send request at http://localhost:8080/crossdomain.xml. Code,
var config = new HttpSelfHostConfiguration("http://localhost:8080");
config.Routes.MapHttpRoute(
"CrossDomain", "crossdomain.xml",
new { controller = "CrossDomain" });
config.Routes.MapHttpRoute(
"API Default", "api/{controller}/{id}",
new { id = RouteParameter.Optional });
using (HttpSelfHostServer server = new HttpSelfHostServer(config))
{
server.OpenAsync().Wait();
Console.WriteLine("Press Enter to quit.");
Console.ReadLine();
}
public class CrossDomainController : ApiController
{
public HttpResponseMessage Get()
{
var response = new HttpResponseMessage();
response.Content = new StreamContent(File.Open(AppDomain.CurrentDomain.BaseDirectory + "crossdomain.xml", FileMode.Open));
return response;
}
}
"And whoever is removed away from the Fire and admitted to Paradise, he indeed is successful." (The Holy Quran)
Excellent Windows VPS Hosting Imran Baloch MVP, MVB, MCP, MCTS, MCPD
Marked as answer by mattj1856 on Mar 27, 2012 07:00 PM
No real benefit in going with this approach. In fact, you now have to go through the binding/action/controller lookup process.
Hopefully, one day we will start to to see an ecosystem around handlers and you will be able to pull a static file handler from nuget.
The downside of message handlers is that it will apply to every request.
IMHO, returning a single static file from action will make sense.
"And whoever is removed away from the Fire and admitted to Paradise, he indeed is successful." (The Holy Quran)
Excellent Windows VPS Hosting Imran Baloch MVP, MVB, MCP, MCTS, MCPD
mattj1856
Member
7 Points
7 Posts
Hosting static content in a self-hosted webapi
Mar 27, 2012 02:42 AM|LINK
I'm using HttpSelfHostServer for my WebApi implementation. I need a Silverlight client to be able to access the API in a cross-domain manner. I've read the posts about adding jsonp and cors support for browser cross-domain access, and those work just fine. However, silverlight doesn't work with these. Instead, it requires a ClientAccessPolicy.xml or a crossdomain.xml file to be created in the root of the web server. See http://msdn.microsoft.com/en-us/library/cc197955(v=vs.95).aspx
If I was hosting on IIS, this wouldn't be an issue, but I can't figure out how to get the self-hosted http server to server up a static xml file. Is there a way to specify a route to a single static file? Or is there some way I can intercept the request for these specific files and produce the xml it needs?
Thanks,
Matt
davebettin
Member
313 Points
94 Posts
Re: Hosting static content in a self-hosted webapi
Mar 27, 2012 03:29 AM|LINK
You could create a message handler to intercept the request and return the file. Read more about handlers here: http://www.asp.net/web-api/overview/working-with-http/http-message-handlers. You will want to make sure to create the response, add your file stream, and return it. Just this alone is naive, but it should help you get started.
@dbettin
mattj1856
Member
7 Points
7 Posts
Re: Hosting static content in a self-hosted webapi
Mar 27, 2012 03:45 AM|LINK
panesofglass
Member
730 Points
237 Posts
Re: Hosting static content in a self-hosted webapi
Mar 27, 2012 05:28 AM|LINK
Any plans to support Socket.SendFile?
imran_ku07
All-Star
45815 Points
7698 Posts
MVP
Re: Hosting static content in a self-hosted webapi
Mar 27, 2012 06:56 PM|LINK
No need for any Message Handler. Send request at http://localhost:8080/crossdomain.xml. Code,
var config = new HttpSelfHostConfiguration("http://localhost:8080"); config.Routes.MapHttpRoute( "CrossDomain", "crossdomain.xml", new { controller = "CrossDomain" }); config.Routes.MapHttpRoute( "API Default", "api/{controller}/{id}", new { id = RouteParameter.Optional }); using (HttpSelfHostServer server = new HttpSelfHostServer(config)) { server.OpenAsync().Wait(); Console.WriteLine("Press Enter to quit."); Console.ReadLine(); } public class CrossDomainController : ApiController { public HttpResponseMessage Get() { var response = new HttpResponseMessage(); response.Content = new StreamContent(File.Open(AppDomain.CurrentDomain.BaseDirectory + "crossdomain.xml", FileMode.Open)); return response; } }Excellent Windows VPS Hosting
Imran Baloch MVP, MVB, MCP, MCTS, MCPD
mattj1856
Member
7 Points
7 Posts
Re: Hosting static content in a self-hosted webapi
Mar 27, 2012 07:03 PM|LINK
Perfect! Excatly what I was looking for. Thanks!
davebettin
Member
313 Points
94 Posts
Re: Hosting static content in a self-hosted webapi
Mar 27, 2012 07:05 PM|LINK
No real benefit in going with this approach. In fact, you now have to go through the binding/action/controller lookup process.
Hopefully, one day we will start to to see an ecosystem around handlers and you will be able to pull a static file handler from nuget.
@dbettin
imran_ku07
All-Star
45815 Points
7698 Posts
MVP
Re: Hosting static content in a self-hosted webapi
Mar 28, 2012 03:23 AM|LINK
The downside of message handlers is that it will apply to every request. IMHO, returning a single static file from action will make sense.
Excellent Windows VPS Hosting
Imran Baloch MVP, MVB, MCP, MCTS, MCPD