I’m having an image file named SampleData with extension as .shweta. Here shweta is my custom extension andthe browser has no information about it. The browser has no idea about how to render this type of file. Please note, this is a static resource placed on wwwroot and has to be served to client. Now quickly open SampleData.shweta in browser and see what happens.


Uhh! This is HTTP 404 Not Found :( .
Now the question is why is there such a discrepancy? Straight forward answer would be – ASP.NET middleware is not able to serve this unknown file type.
Serving unknown content type
In order to serve files of an unknown type, we have to tell the browser which type of file is this. Fortunately, ASP.NET provides us with two useful properties named ServeUnknownFileTypes and DefaultContentType which will do this task for us.
Now if the user glances at a file whose content type is unknown, the browser will treat it as an image and render it accordingly as shown below:
The next interesting question is, what if there are multiple types of files having an unknown to ASP.NET? No worries, ASP.NET Core has solution for that also.
Serving multiple unknown content types
It provides a class named FileExtensionContentTypeProvider. Hence any custom content type has to be first added in a collection of this class with proper mapping of extension MIME type. Below is the sample code for achieving this:
More information about FileExtensionContentTypeProvider class is given here at github and more information about application/x-msdownload is given here.






Asfend YarPosted Feb 29, 2016, 8:40 AM EST
good
Pramod ThakurPosted Feb 4, 2016, 9:44 PM EST
Nice..
Ajeet MishraPosted Feb 4, 2016, 9:10 PM EST
nice
Raja TPosted Feb 4, 2016, 5:49 PM EST
Nice,Thanks for sharing
Vivek KumarPosted Feb 4, 2016, 9:35 AM EST
Nice article
Mohammed IbrahimPosted Feb 4, 2016, 8:47 AM EST
nice
Debasis SahaTOP 100Posted Feb 4, 2016, 4:54 AM EST
Nice Share..
Vaikesh K PPosted Feb 4, 2016, 2:55 AM EST
Nice one
Sibeesh VenuTOP 100Posted Feb 3, 2016, 7:46 PM EST
Nice Share
Santhakumar MunuswamyPosted Feb 3, 2016, 10:06 AM EST
Thanks for nice share
Ankit SaxenaPosted Feb 3, 2016, 6:09 AM EST
Very nice explanation.
Nanddeep NachanTOP 100Posted Feb 2, 2016, 8:14 PM EST
Nice share
Amit Kumar SinghPosted Feb 2, 2016, 7:57 PM EST
Nice
Saineshwar BageriPosted Feb 2, 2016, 7:33 PM EST
Nice one
Shubham KumarPosted Feb 2, 2016, 7:28 PM EST
nice
Sibeesh VenuTOP 100Posted Feb 2, 2016, 7:28 PM EST
Nice Share
Debasis SahaTOP 100Posted Feb 2, 2016, 6:44 PM EST
Nice one..
Pramod ThakurPosted Feb 2, 2016, 4:17 PM EST
thanx for sharing..
Mohammed IbrahimPosted Feb 2, 2016, 9:14 AM EST
nice