3

ASP.NET MVC 4プロジェクトに「パブリック」フォルダーを与える方法はありますか?これは、Ruby on Railsのように、アプリケーションルートのように機能しますか?

ASP.NET MVCには通常、スタイルシート、スクリプト、画像などの静的に提供されるファイルを配置できる「コンテンツ」フォルダーが付属しています。ただし、これらのファイルのURLには、たとえばContentフォルダーを含める必要があります<img src="/Content/logo.png">。Contentフォルダーをアプリケーションのルートにして、代わりにこれを使用できるようにすることはできます<img src="/logo.png">か?

4

1 に答える 1

1

以前にこれを考えなかった理由はわかりませんが、IISURLRewriteを使用してこれを実現できました。次のルールは、静的ファイルが存在する「Public」という名前のフォルダーを想定しています。もちろん、フォルダには任意の名前を付けることができます。

<!-- Any direct references to files in the Public folder should be
     301 redirected to maintain canonical URLs. This is optional. -->
<rule name="Public folder canonical path" stopProcessing="true">
  <match url="^public/(.*)" />
  <conditions logicalGrouping="MatchAll" trackAllCaptures="false" />
  <action type="Redirect" url="{R:1}" appendQueryString="true" />
</rule>

<!-- Any URL that points to an existing file after prepending "Public" to it
     will serve that file. For example, if a file exists at /Public/style.css
     then the URL /style.css will serve that file. Likewise, if a file exists
     at /Public/images/logo.png, then the URL /images/logo.png will serve
     that file. Files in the Public folder will take precedence over files in
     the application root, so if a file /Public/script.js exists and a file
     /script.js exists, only the /Public/script.js file will be served. This
     also takes precedence over MVC routes. -->
<rule name="Public folder" stopProcessing="true">
  <match url=".+" />
  <conditions logicalGrouping="MatchAll" trackAllCaptures="false">
    <add input="{URL}" pattern="^/Public/" negate="true" />
    <add input="{DOCUMENT_ROOT}/Public{URL}" matchType="IsFile" />
  </conditions>
  <action type="Rewrite" url="Public/{R:0}" appendQueryString="true" />
</rule>
于 2012-08-31T06:42:28.570 に答える