aspnetcore-angular-universal
aspnetcore-angular-universal copied to clipboard
Adding mime types produces 404
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.
- Clone/Download repo.
- Add an empty text file under wwwroot/assets. Rename it as sample.properties
- Add another empty file under same directory. Name it sample.json
- Request both files via browser. sample.json produces no error
- Requesting sample.properties produces 404 (
GET http://localhost:63554/assets/sample.properties 404 (Not Found))
What did I try.
- Added mime map on web.config of aspnet core project
- Added runAllManagedModulesForAllRequests to web.config
- 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.
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
};
}
});