aspnetcore-angular-universal icon indicating copy to clipboard operation
aspnetcore-angular-universal copied to clipboard

Adding mime types produces 404

Open codehippie1 opened this issue 5 years ago • 1 comments

I have been trying to get mime maps working on this repository.

Every http request to unknown mime types (On IIS Express and Azure web apps) results in 404 error.

Steps.

  1. Clone/Download repo.
  2. Add an empty text file under wwwroot/assets. Rename it as sample.properties
  3. Add another empty file under same directory. Name it sample.json
  4. Request both files via browser. sample.json produces no error
  5. Requesting sample.properties produces 404 (GET http://localhost:63554/assets/sample.properties 404 (Not Found))

What did I try.

  1. Added mime map on web.config of aspnet core project
  2. Added runAllManagedModulesForAllRequests to web.config
  3. Executed appcmd set config /section:staticContent /+[fileExtension='properties',mimeType='application/octet-stream'] from IIS Express folder(C:\Program Files(x86)\IIS Express) in command line(as admin)

Here is full web.config

<system.webServer>
    <modules runAllManagedModulesForAllRequests="true" />
    <handlers>
      <add name="aspNetCore" path="*" verb="*" modules="AspNetCoreModule" resourceType="Unspecified" />
    </handlers>
    <aspNetCore processPath="%LAUNCHER_PATH%" arguments="%LAUNCHER_ARGS%" stdoutLogEnabled="false" stdoutLogFile=".\logs\stdout" forwardWindowsAuthToken="false">
      <environmentVariables />
    </aspNetCore>
    <staticContent>
      <remove fileExtension=".properties" />
      <mimeMap fileExtension=".properties" mimeType="application/octet-stream" />
      <remove fileExtension=".txtabcd" />
      <mimeMap fileExtension=".txtabcd" mimeType="application/text" />
    </staticContent>
  </system.webServer>

Interestingly enough, deploying this to an azure app service still produces this error. @MarkPieszak Have you encountered a similar error on unusual mime types before?

PS: Default VS 2017 angular template works without adding mimemap for properties file.

codehippie1 avatar Jul 01 '19 19:07 codehippie1

I found the cause of this issue

Aspnet core does not honor webserver->staticcontent->mimetypes as described in these posts. In short, static file requests in this case does not reach IIS express to be served, hence web.config modifications has no effect.

https://stackoverflow.com/questions/51770084/how-to-add-mime-types-in-asp-net-core https://docs.microsoft.com/en-us/aspnet/core/fundamentals/static-files?view=aspnetcore-2.1&tabs=aspnetcore2x#fileextensioncontenttypeprovider

The below modification on Startup.cs will suffice

var provider = new FileExtensionContentTypeProvider();
provider.Mappings.Remove(".properties");
provider.Mappings[".properties"] = "application/octet-stream";

app.UseStaticFiles (new StaticFileOptions () {
                ContentTypeProvider = provider, // <----- HERE
                OnPrepareResponse = c => {
                    //Do not add cache to json files. We need to have new versions when we add new translations.
                    c.Context.Response.GetTypedHeaders ().CacheControl = !c.Context.Request.Path.Value.Contains (".json")
                        ? new CacheControlHeaderValue () {
                            MaxAge = TimeSpan.FromDays (30) // Cache everything except json for 30 days
                        }
                        : new CacheControlHeaderValue () {
                            MaxAge = TimeSpan.FromMinutes (15) // Cache json for 15 minutes
                        };
                }
            });

codehippie1 avatar Jul 01 '19 21:07 codehippie1