0

I am triyng to run a little MelonJS Game in a ASP.NET MVC 4 Razor Page. First of all, I think it is possible to fix this without any MelonJS knowledge (only MVC 4).

The problem is:

At one moment, MelonJS need to load some files from the server (I put the files in Content/data/[..]/file.ext). To do this it performs for each file a HTTP GET on localhost:XXXX/%EveryThingIWant/file.ext%.

Of course, it fails. I try to enable DirectoryBrowsing but it did not solve the problem. In order to get it working quickly, I did this (I'm not proud of it, it's just a quick fix):

I create a new action in one of my controllers:

    //
    // GET: /Game/Data

    public FileResult Data(string path)
    {
        string physicalPath = Server.MapPath("~/Content/" + path);

        byte[] fileByte = null;

        using (FileStream fs = new FileStream(physicalPath, FileMode.Open))
        {
            fileByte = new byte[fs.Length];

            fs.Read(fileByte, 0, (int)fs.Length);
        }

        var result = new FileContentResult(fileByte, "tmx");

        return result;
    }

And I set the %EveryThingIWant/file.ext% to "Game/Data?path=[..]/file.ext".

It works but I am wondering if there is not a better solution to perform that. Put the file in an other folder? I tried to enable DirectoryBrowsing and adding a MIME type but I failed util now. It is possible?

Dan
  • 9,391
  • 5
  • 41
  • 73
fharreau
  • 2,105
  • 1
  • 23
  • 46
  • When you say it fails, what is the error you get? – asymptoticFault Sep 01 '13 at 16:29
  • 1
    You can see examples of some more-concise approaches to this problem in the answers here: http://stackoverflow.com/questions/186062/can-an-asp-net-mvc-controller-return-an-image – David Tansey Sep 01 '13 at 16:39
  • @asymptoticFault : if I don't add my dirty solution, as MelonJS performs a HTTP GET on nothing, it return a 404. I am looking th other links, thanks ! – fharreau Sep 01 '13 at 17:16
  • My solution works ! It can be improved (with try/catch, etc), of course, but I am looking for another solution. I wonder there is one with no code but about configuration. For example, enabling DirectoryBrowsing and adding a MIME type but I failed util now. – fharreau Sep 01 '13 at 17:31

2 Answers2

1

Have a look at this article on how to return a file as a response. It covers the FileResult and its subclasses and the inherited File methods from the base controller.

asymptoticFault
  • 4,491
  • 2
  • 19
  • 24
  • My solution works ! It can be improved (with try/catch, etc), of course, but I am looking for another solution. I wonder there is one with no code but about configuration. For example, enabling DirectoryBrowsing and adding a MIME type but I failed util now. – fharreau Sep 01 '13 at 17:30
0

I don't know if it's the good way but this is what I've finally done :

-I put the data in a specific folder (I chose App_Data but I don't know if it's the good place for that)

-I put a Web.config in this folder with that content :

<?xml version="1.0"?>
<configuration>
  <system.webServer>
    <directoryBrowse enabled="true" />
    <staticContent>
      <mimeMap fileExtension=".tmx" mimeType="text/plain" />
    </staticContent>
  </system.webServer>
</configuration>

(I guess I have to specify all custom extension)

-In my case (App_Data folder), I had to add in the global Web.config :

<?xml version="1.0"?>
<configuration>
  <security>
    <requestFiltering>
      <hiddenSegments>
        <remove segment="App_Data" />
      </hiddenSegments>
    </requestFiltering>
  </security>
</configuration>

With these modifications, the HTTP GET on my resources works well.

fharreau
  • 2,105
  • 1
  • 23
  • 46