0

我正在尝试在 ASP.NET MVC 4 Razor 页面中运行一个小的 MelonJS 游戏。首先,我认为可以在没有任何 MelonJS 知识的情况下解决这个问题(仅限 MVC 4)。

问题是:

在某一时刻,MelonJS 需要从服务器加载一些文件(我将文件放在 Content/data/[..]/file.ext 中)。为此,它为每个文件执行一个 HTTP GETlocalhost:XXXX/%EveryThingIWant/file.ext%.

当然,它失败了。我尝试启用 DirectoryBrowsing 但它没有解决问题。为了让它快速工作,我这样做了(我并不为此感到自豪,这只是一个快速修复):

我在我的一个控制器中创建了一个新操作:

    //
    // 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;
    }

我将 %EveryThingIWant/file.ext% 设置为“Game/Data?path=[..]/file.ext”。

它有效,但我想知道是否没有更好的解决方案来执行此操作。把文件放在其他文件夹?我尝试启用 DirectoryBrowsing 并添加 MIME 类型,但我现在失败了。有可能的?

4

2 回答 2

1

查看这篇文章,了解如何将文件作为响应返回。它涵盖了FileResult它及其子类以及File从基本控制器继承的方法。

于 2013-09-01T16:53:11.017 回答
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.

于 2013-09-02T08:30:10.127 回答