Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Adding mime types produces 404 #732

Open
codehippie1 opened this issue Jul 1, 2019 · 1 comment
Open

Adding mime types produces 404 #732

codehippie1 opened this issue Jul 1, 2019 · 1 comment

Comments

@codehippie1
Copy link

codehippie1 commented Jul 1, 2019

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 codehippie1 changed the title Adding mime types Adding mime types produces 404 Jul 1, 2019
@codehippie1
Copy link
Author

codehippie1 commented Jul 1, 2019

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant