Dart - Circular dependency while initializing static field - dart

I'm new to dart and just encountered an issue which I don't understand yet.
I wrote this class:
class Currency {
final String symbol;
final String name;
// constants for all available Currencies
static const Currency EURO = const Currency._euro();
static const Currency POUND = const Currency._pound();
static const Currency DOLLAR = const Currency._dollar();
// All available currencies as a list
static const List<Currency> CURRENCIES = const [
EURO,
POUND,
DOLLAR,
];
// Default constructor
Currency(this.symbol, this.name);
// Named constructors
const Currency._euro() : this('€', 'Euro');
const Currency._pound() : this('£', 'British Pound');
const Currency._dollar() : this('\$', 'US Dollar');
// toString()
#override
String toString() => '$symbol ($name)';
}
When using this class, for example with the statement below I get a "Circular dependency while initializing static field"-error.
Currency currency = Currency.EURO;
Could anyone explain to me what is going on?

I can't reproduce your error, but a const was missing before the constructor you redirect others to
const Currency(this.symbol, this.name);

Related

Difference between const constructor and cost?

I have the following code snippet:
class ImmutablePoint {
final double x, y;
const ImmutablePoint(this.x, this.y);
}
void main() {
var i = const ImmutablePoint(4,6);
print(i.x);
}
As you can see, a const constructor is defined in the class. However, what is the difference between const and const constructor?
What is the difference when I change from var i = const ImmutablePoint(4,6); to var i = ImmutablePoint(4,6);?
The point is, sometimes I see in flutter example const Text("FFF"), although a const constructor is already defined for the text.
A class with a const constructor only allows you to create const instances of that class, but it does not make all instances const.
That is controlled at the constructor invocation site - the const constructor is only used within a const context. A const context is basically inferred by any precedingconst keyword, the following all would use the const constructor:
var foo = const ImmutablePoint(1,2);
const foo = ImmutablePoint(1,2);
var foos = const [ImmutablePoint(1,2)];
But the following would not, as it is not in a const context:
var foo = ImmutablePoint(1,2);

How to extend from const constructor

class and const gives a nice possibility to define structured constants. Now I try to extend from one, works ok. Try to use a constant from the inherited class, it works, still I only manage to get through with final instead of const. No big deal in my project, but is it possible to have it const?
class A {
final String name;
final int value;
const A(final String this.name, final this.value);
static const A ONE = const A('One', 1);
}
class B extends A {
const B(final String name, final int val) : super(name, val);
static const B B_ONE = const B('One', 1);//Ok
static const A A_ONE = A.ONE;//Ok
static final B BA_ONE = new B(A.ONE.name, A.ONE.value);//Ok
static const B BA_ONE_CONST = const B(A.ONE);//No B constructor!?!
}
Your B class indeed does not have a constructor that takes a single A instance. You haven't declared one, and you can't do it directly without a little workaround.
The problem is that B extends A, it doesn't just contain an A instance. So, if you want to start with an A instance (like A.ONE) and create a B instance from it, where B extends A and has its own name and value fields, you'll have to extract the name and value from the A to create the new object ... and you can't do property extraction in a const expression. So, that's a no-go.
What you can do is to have a different implementation of B that gets its values directly from an A instance, and then create a constant from that:
class B extends A {
const B(String name, int value) : super(name, value);
const factory B.fromA(A instance) = _SecretB;
...
static const B BA_ONE_CONST = const B.fromA(A.ONE);
}
class _SecretB implements B {
final A _instance;
const _SecretB(this._instance);
String get name => _instance.name;
int get value => _instance.value;
}
If you are the only one using the B.fromA constructor, you could just remove it and call the _SecretB constructor directly. If you want to expose it to other clients of your class, you can make it a const factory constructor like here.

how to get values from a const object?

I created a const object at app.config.dart with the following code:
const configObj = const {
'webServer': const {
'appBaseHref' : "/"
},
'auth0': const {
'apiKey': "foo",
'domain': "bar",
'callbackUrl': "callback"
}
};
now in my main dart file I import the app.config.dart and I try to get the values there and now idea how to do that. configObj.auth0.apiKey produces the error EXCEPTION: Class 'ImmutableMap' has no instance getter 'auth0'.
so how do I do this ?
thanks!
Dart doesn't support to access map entries with .
It should be:
configObj['auth0']['apiKey'];
Alternatively you can create classes for your configuration like
class WebServerConfig {
final String appBaseHref;
const WebServerConfig(this.appBaseHref);
}
class Auth0Config {
final String apiKey;
final String domain;
final String callbackUrl;
const Auth0(this.apiKey, this.domain, this.callbackUrl);
}
class MyConfig {
final WebServerConfig webServer;
final Auth0Config auth0;
const MyConfig(this.webServer, this.auth0);
}
const configObj = const MyConfig(
const WebServerConfig("/"),
const Auth0Config(
"foo",
"bar",
"callback"
)
);
This way you also get proper auto-completion when you access the config properties and can use the simple . notation to access properties.

How can I build an enum with Dart? [duplicate]

This question already has answers here:
Does Dart support enumerations?
(8 answers)
Closed 7 years ago.
The Dart language does not have enums (yet??). What is the proper or idiomatic way to construct an enum, at least until a language feature arrives?
Dart now has support for enums.
The rest of this answer is for Dart <= 1.8. If using > 1.8, use Dart's formal support for enums (explained in another answer).
It's true, the Dart language does not (yet?) have enums. There is an open issue for it.
In the meantime, here is an idiomatic Dart snippet to create your own enum.
class Enum {
final _value;
const Enum._internal(this._value);
toString() => 'Enum.$_value';
static const FOO = const Enum._internal('FOO');
static const BAR = const Enum._internal('BAR');
static const BAZ = const Enum._internal('BAZ');
}
Using const constructors means you can use this enum in a switch. Here's an example:
class Fruits {
final _value;
const Fruits._internal(this._value);
toString() => 'Enum.$_value';
static const APPLE = const Fruits._internal('APPLE');
static const PEAR = const Fruits._internal('PEAR');
static const BANANA = const Fruits._internal('BANANA');
}
void main() {
var yummy = Fruits.BANANA;
switch (yummy) {
case Fruits.APPLE:
print('an apple a day');
break;
case Fruits.PEAR:
print('genus Pyrus in the family Rosaceae');
break;
case Fruits.BANANA:
print('open from the bottom, it is easier');
break;
}
}
With r41815 Dart got native Enum support see http://dartbug.com/21416 and can be used like
enum Status {
none,
running,
stopped,
paused
}
void main() {
print(Status.values);
Status.values.forEach((v) => print('value: $v, index: ${v.index}'));
print('running: ${Status.running}, ${Status.running.index}');
print('running index: ${Status.values[1]}');
}
[Status.none, Status.running, Status.stopped, Status.paused]
value: Status.none, index: 0
value: Status.running, index: 1
value: Status.stopped, index: 2
value: Status.paused, index: 3
running: Status.running, 1
running index: Status.running
A limitation is that it is not possibly to set custom values for an enum item, they are automatically numbered.
More details at in this draft https://www.dartlang.org/docs/spec/EnumsTC52draft.pdf
I use a little bit simpler version of the Enum class in Dart Web Toolkit:
/**
* Emulation of Java Enum class.
*
* Example:
*
* class Meter<int> extends Enum<int> {
*
* const Meter(int val) : super (val);
*
* static const Meter HIGH = const Meter(100);
* static const Meter MIDDLE = const Meter(50);
* static const Meter LOW = const Meter(10);
* }
*
* and usage:
*
* assert (Meter.HIGH, 100);
* assert (Meter.HIGH is Meter);
*/
abstract class Enum<T> {
final T _value;
const Enum(this._value);
T get value => _value;
}
I like top-level constants for my enums. You can use imports to fix any collisions. This makes using the enums much less verbose.
i.e.
if (m == high) {}
instead of:
if (m == Meter.high) {}
Enum definition:
class Meter<int> extends Enum<int> {
const Meter(int val) : super (val);
}
const Meter high = const Meter(100);
const Meter middle = const Meter(50);
const Meter low = const Meter(10);

ASP MVC - Are there any constants for the default content types?

Is there a set of constants in the framework for all the standard content types?
Taking it as a resounding no, I wrote my own. That's what Sundays were made for.
///<summary>Used to denote the encoding necessary for files containing JavaScript source code. The alternative MIME type for this file type is text/javascript.</summary>
public const string ApplicationXJavascript = "application/x-javascript";
///<summary>24bit Linear PCM audio at 8-48kHz, 1-N channels; Defined in RFC 3190</summary>
public const string AudioL24 = "audio/L24";
///<summary>Adobe Flash files for example with the extension .swf</summary>
public const string ApplicationXShockwaveFlash = "application/x-shockwave-flash";
///<summary>Arbitrary binary data.[5] Generally speaking this type identifies files that are not associated with a specific application. Contrary to past assumptions by software packages such as Apache this is not a type that should be applied to unknown files. In such a case, a server or application should not indicate a content type, as it may be incorrect, but rather, should omit the type in order to allow the recipient to guess the type.[6]</summary>
public const string ApplicationOctetStream = "application/octet-stream";
///<summary>Atom feeds</summary>
public const string ApplicationAtomXml = "application/atom+xml";
///<summary>Cascading Style Sheets; Defined in RFC 2318</summary>
public const string TextCss = "text/css";
///<summary>commands; subtype resident in Gecko browsers like Firefox 3.5</summary>
public const string TextCmd = "text/cmd";
///<summary>Comma-separated values; Defined in RFC 4180</summary>
public const string TextCsv = "text/csv";
///<summary>deb (file format), a software package format used by the Debian project</summary>
public const string ApplicationXDeb = "application/x-deb";
///<summary>Defined in RFC 1847</summary>
public const string MultipartEncrypted = "multipart/encrypted";
///<summary>Defined in RFC 1847</summary>
public const string MultipartSigned = "multipart/signed";
///<summary>Defined in RFC 2616</summary>
public const string MessageHttp = "message/http";
///<summary>Defined in RFC 4735</summary>
public const string ModelExample = "model/example";
///<summary>device-independent document in DVI format</summary>
public const string ApplicationXDvi = "application/x-dvi";
///<summary>DTD files; Defined by RFC 3023</summary>
public const string ApplicationXmlDtd = "application/xml-dtd";
///<summary>ECMAScript/JavaScript; Defined in RFC 4329 (equivalent to application/ecmascript but with looser processing rules) It is not accepted in IE 8 or earlier - text/javascript is accepted but it is defined as obsolete in RFC 4329. The "type" attribute of the <script> tag in HTML5 is optional and in practice omitting the media type of JavaScript programs is the most interoperable solution since all browsers have always assumed the correct default even before HTML5.</summary>
public const string ApplicationJavascript = "application/javascript";
///<summary>ECMAScript/JavaScript; Defined in RFC 4329 (equivalent to application/javascript but with stricter processing rules)</summary>
public const string ApplicationEcmascript = "application/ecmascript";
///<summary>EDI EDIFACT data; Defined in RFC 1767</summary>
public const string ApplicationEdifact = "application/EDIFACT";
///<summary>EDI X12 data; Defined in RFC 1767</summary>
public const string ApplicationEdiX12 = "application/EDI-X12";
///<summary>Email; Defined in RFC 2045 and RFC 2046</summary>
public const string MessagePartial = "message/partial";
///<summary>Email; EML files, MIME files, MHT files, MHTML files; Defined in RFC 2045 and RFC 2046</summary>
public const string MessageRfc822 = "message/rfc822";
///<summary>Extensible Markup Language; Defined in RFC 3023</summary>
public const string TextXml = "text/xml";
///<summary>Extensible Markup Language; Defined in RFC 3023</summary>
public const string ApplicationXml = "application/xml";
///<summary>Flash video (FLV files)</summary>
public const string VideoXFlv = "video/x-flv";
///<summary>GIF image; Defined in RFC 2045 and RFC 2046</summary>
public const string ImageGif = "image/gif";
///<summary>GoogleWebToolkit data</summary>
public const string TextXGwtRpc = "text/x-gwt-rpc";
///<summary>Gzip</summary>
public const string ApplicationXGzip = "application/x-gzip";
///<summary>HTML; Defined in RFC 2854</summary>
public const string TextHtml = "text/html";
///<summary>ICO image; Registered[9]</summary>
public const string ImageVndMicrosoftIcon = "image/vnd.microsoft.icon";
///<summary>IGS files, IGES files; Defined in RFC 2077</summary>
public const string ModelIges = "model/iges";
///<summary>IMDN Instant Message Disposition Notification; Defined in RFC 5438</summary>
public const string MessageImdnXml = "message/imdn+xml";
///<summary>JavaScript Object Notation JSON; Defined in RFC 4627</summary>
public const string ApplicationJson = "application/json";
///<summary>JavaScript Object Notation (JSON) Patch; Defined in RFC 6902</summary>
public const string ApplicationJsonPatch = "application/json-patch+json";
///<summary>JavaScript - Defined in and obsoleted by RFC 4329 in order to discourage its usage in favor of application/javascript. However,text/javascript is allowed in HTML 4 and 5 and, unlike application/javascript, has cross-browser support. The "type" attribute of the <script> tag in HTML5 is optional and there is no need to use it at all since all browsers have always assumed the correct default (even in HTML 4 where it was required by the specification).</summary>
[Obsolete]
public const string TextJavascript = "text/javascript";
///<summary>JPEG JFIF image; Associated with Internet Explorer; Listed in ms775147(v=vs.85) - Progressive JPEG, initiated before global browser support for progressive JPEGs (Microsoft and Firefox).</summary>
public const string ImagePjpeg = "image/pjpeg";
///<summary>JPEG JFIF image; Defined in RFC 2045 and RFC 2046</summary>
public const string ImageJpeg = "image/jpeg";
///<summary>jQuery template data</summary>
public const string TextXJqueryTmpl = "text/x-jquery-tmpl";
///<summary>KML files (e.g. for Google Earth)</summary>
public const string ApplicationVndGoogleEarthKmlXml = "application/vnd.google-earth.kml+xml";
///<summary>LaTeX files</summary>
public const string ApplicationXLatex = "application/x-latex";
///<summary>Matroska open media format</summary>
public const string VideoXMatroska = "video/x-matroska";
///<summary>Microsoft Excel 2007 files</summary>
public const string ApplicationVndOpenxmlformatsOfficedocumentSpreadsheetmlSheet = "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet";
///<summary>Microsoft Excel files</summary>
public const string ApplicationVndMsExcel = "application/vnd.ms-excel";
///<summary>Microsoft Powerpoint 2007 files</summary>
public const string ApplicationVndOpenxmlformatsOfficedocumentPresentationmlPresentation = "application/vnd.openxmlformats-officedocument.presentationml.presentation";
///<summary>Microsoft Powerpoint files</summary>
public const string ApplicationVndMsPowerpoint = "application/vnd.ms-powerpoint";
///<summary>Microsoft Word 2007 files</summary>
public const string ApplicationVndOpenxmlformatsOfficedocumentWordprocessingmlDocument = "application/vnd.openxmlformats-officedocument.wordprocessingml.document";
///<summary>Microsoft Word files[15]</summary>
public const string ApplicationMsword = "application/msword";
///<summary>MIME Email; Defined in RFC 2045 and RFC 2046</summary>
public const string MultipartAlternative = "multipart/alternative";
///<summary>MIME Email; Defined in RFC 2045 and RFC 2046</summary>
public const string MultipartMixed = "multipart/mixed";
///<summary>MIME Email; Defined in RFC 2387 and used by MHTML (HTML mail)</summary>
public const string MultipartRelated = "multipart/related";
///<summary>MIME Webform; Defined in RFC 2388</summary>
public const string MultipartFormData = "multipart/form-data";
/// <summary>Body contains a URL-encoded query string as per RFC 1867</summary>
public const string ApplicationWwwFormUrlEncoded = "application/x-www-form-urlencoded";
///<summary>Mozilla XUL files</summary>
public const string ApplicationVndMozillaXulXml = "application/vnd.mozilla.xul+xml";
///<summary>MP3 or other MPEG audio; Defined in RFC 3003</summary>
public const string AudioMpeg = "audio/mpeg";
///<summary>MP4 audio</summary>
public const string AudioMp4 = "audio/mp4";
///<summary>MP4 video; Defined in RFC 4337</summary>
public const string VideoMp4 = "video/mp4";
///<summary>MPEG-1 video with multiplexed audio; Defined in RFC 2045 and RFC 2046</summary>
public const string VideoMpeg = "video/mpeg";
///<summary>MSH files, MESH files; Defined in RFC 2077, SILO files</summary>
public const string ModelMesh = "model/mesh";
///<summary>mulaw audio at 8 kHz, 1 channel; Defined in RFC 2046</summary>
public const string AudioBasic = "audio/basic";
///<summary>Ogg Theora or other video (with audio); Defined in RFC 5334</summary>
public const string VideoOgg = "video/ogg";
///<summary>Ogg Vorbis, Speex, Flac and other audio; Defined in RFC 5334</summary>
public const string AudioOgg = "audio/ogg";
///<summary>Ogg, a multimedia bitstream container format; Defined in RFC 5334</summary>
public const string ApplicationOgg = "application/ogg";
///<summary>OP</summary>
public const string ApplicationXopXml = "application/xop+xml";
///<summary>OpenDocument Graphics; Registered[14]</summary>
public const string ApplicationVndOasisOpendocumentGraphics = "application/vnd.oasis.opendocument.graphics";
///<summary>OpenDocument Presentation; Registered[13]</summary>
public const string ApplicationVndOasisOpendocumentPresentation = "application/vnd.oasis.opendocument.presentation";
///<summary>OpenDocument Spreadsheet; Registered[12]</summary>
public const string ApplicationVndOasisOpendocumentSpreadsheet = "application/vnd.oasis.opendocument.spreadsheet";
///<summary>OpenDocument Text; Registered[11]</summary>
public const string ApplicationVndOasisOpendocumentText = "application/vnd.oasis.opendocument.text";
///<summary>p12 files</summary>
public const string ApplicationXPkcs12 = "application/x-pkcs12";
///<summary>p7b and spc files</summary>
public const string ApplicationXPkcs7Certificates = "application/x-pkcs7-certificates";
///<summary>p7c files</summary>
public const string ApplicationXPkcs7Mime = "application/x-pkcs7-mime";
///<summary>p7r files</summary>
public const string ApplicationXPkcs7Certreqresp = "application/x-pkcs7-certreqresp";
///<summary>p7s files</summary>
public const string ApplicationXPkcs7Signature = "application/x-pkcs7-signature";
///<summary>Portable Document Format, PDF has been in use for document exchange on the Internet since 1993; Defined in RFC 3778</summary>
public const string ApplicationPdf = "application/pdf";
///<summary>Portable Network Graphics; Registered,[8] Defined in RFC 2083</summary>
public const string ImagePng = "image/png";
///<summary>PostScript; Defined in RFC 2046</summary>
public const string ApplicationPostscript = "application/postscript";
///<summary>QuickTime video; Registered[10]</summary>
public const string VideoQuicktime = "video/quicktime";
///<summary>RAR archive files</summary>
public const string ApplicationXRarCompressed = "application/x-rar-compressed";
///<summary>RealAudio; Documented in RealPlayer Customer Support Answer 2559</summary>
public const string AudioVndRnRealaudio = "audio/vnd.rn-realaudio";
///<summary>Resource Description Framework; Defined by RFC 3870</summary>
public const string ApplicationRdfXml = "application/rdf+xml";
///<summary>RSS feeds</summary>
public const string ApplicationRssXml = "application/rss+xml";
///<summary>SOAP; Defined by RFC 3902</summary>
public const string ApplicationSoapXml = "application/soap+xml";
///<summary>StuffIt archive files</summary>
public const string ApplicationXStuffit = "application/x-stuffit";
///<summary>SVG vector image; Defined in SVG Tiny 1.2 Specification Appendix M</summary>
public const string ImageSvgXml = "image/svg+xml";
///<summary>Tag Image File Format (only for Baseline TIFF); Defined in RFC 3302</summary>
public const string ImageTiff = "image/tiff";
///<summary>Tarball files</summary>
public const string ApplicationXTar = "application/x-tar";
///<summary>Textual data; Defined in RFC 2046 and RFC 3676</summary>
public const string TextPlain = "text/plain";
///<summary>TrueType Font No registered MIME type, but this is the most commonly used</summary>
public const string ApplicationXFontTtf = "application/x-font-ttf";
///<summary>vCard (contact information); Defined in RFC 6350</summary>
public const string TextVcard = "text/vcard";
///<summary>Vorbis encoded audio; Defined in RFC 5215</summary>
public const string AudioVorbis = "audio/vorbis";
///<summary>WAV audio; Defined in RFC 2361</summary>
public const string AudioVndWave = "audio/vnd.wave";
///<summary>Web Open Font Format; (candidate recommendation; use application/x-font-woff until standard is official)</summary>
public const string ApplicationFontWoff = "application/font-woff";
///<summary>WebM Matroska-based open media format</summary>
public const string VideoWebm = "video/webm";
///<summary>WebM open media format</summary>
public const string AudioWebm = "audio/webm";
///<summary>Windows Media Audio Redirector; Documented in Microsoft help page</summary>
public const string AudioXMsWax = "audio/x-ms-wax";
///<summary>Windows Media Audio; Documented in Microsoft KB 288102</summary>
public const string AudioXMsWma = "audio/x-ms-wma";
///<summary>Windows Media Video; Documented in Microsoft KB 288102</summary>
public const string VideoXMsWmv = "video/x-ms-wmv";
///<summary>WRL files, VRML files; Defined in RFC 2077</summary>
public const string ModelVrml = "model/vrml";
///<summary>X3D ISO standard for representing 3D computer graphics, X3D XML files</summary>
public const string ModelX3DXml = "model/x3d+xml";
///<summary>X3D ISO standard for representing 3D computer graphics, X3DB binary files</summary>
public const string ModelX3DBinary = "model/x3d+binary";
///<summary>X3D ISO standard for representing 3D computer graphics, X3DV VRML files</summary>
public const string ModelX3DVrml = "model/x3d+vrml";
///<summary>XHTML; Defined by RFC 3236</summary>
public const string ApplicationXhtmlXml = "application/xhtml+xml";
///<summary>ZIP archive files; Registered[7]</summary>
public const string ApplicationZip = "application/zip";
Update
Stumbled upon this great resource today.
https://www.iana.org/assignments/media-types/media-types.xhtml
Update 22-03-2020
As Jon and James White mentioned on comments, you can check:
System.Net.Mime.MediaTypeNames
You could use:
System.Net.Mime.MediaTypeNames.Application
System.Net.Mime.MediaTypeNames.Image
System.Net.Mime.MediaTypeNames.Text
For example, System.Net.Mime.MediaTypeNames.Image.Gif is equal to "image/gif".
Here is the complete list of default content type constants in .NET Framework base class library (BCL). You will have to add reference to System.Net.Http.dll in your project to use these constants.
System.Net.Mime.MediaTypeNames.Application.Octet for application/octet-stream
System.Net.Mime.MediaTypeNames.Application.Pdf for application/pdf
System.Net.Mime.MediaTypeNames.Application.Rtf for application/rtf
System.Net.Mime.MediaTypeNames.Application.Soap for application/soap+xml
System.Net.Mime.MediaTypeNames.Application.Zip for application/zip
System.Net.Mime.MediaTypeNames.Image.Gif for image/gif
System.Net.Mime.MediaTypeNames.Image.Jpeg for image/jpeg
System.Net.Mime.MediaTypeNames.Image.Tiff for image/tiff
System.Net.Mime.MediaTypeNames.Text.Html for text/html
System.Net.Mime.MediaTypeNames.Text.Plain for text/plain
System.Net.Mime.MediaTypeNames.Text.RichText for text/richtext
System.Net.Mime.MediaTypeNames.Text.Xml for text/xml
Update[12-Aug-2021]:
A new set of constants are now available in System.Net.Mime namespace. This namespace is part of System.Net.Mail.dll which is referenced by default whenever you create a new project targeting .NET v5. This DLL is part of .NET v5 (erstwhile .NET Core v5) release.
MediaTypeNames.Application.Json
MediaTypeNames.Application.Octet
MediaTypeNames.Application.Pdf
MediaTypeNames.Application.Rtf
MediaTypeNames.Application.Soap
MediaTypeNames.Application.Xml
MediaTypeNames.Application.Zip
MediaTypeNames.Image.Gif
MediaTypeNames.Image.Jpeg
MediaTypeNames.Image.Tiff
MediaTypeNames.Text.Html
MediaTypeNames.Text.Plain
MediaTypeNames.Text.RichText
MediaTypeNames.Text.Xml
Note: .NET v5 is not a new release of .NET Framework. Instead it is the next major release of .NET following .NET Core v3.1. .NET is new name of .NET Core. Core branding has been removed.
In the new ASP.NET Core MimeMapping can be used with the filename:
MimeMapping.GetMimeMapping("1.jpg")
Should return image/jpeg.
All the mappings can be found here and it is also possible to add your own (see e.g. this answer).
Here's the official on going list if interested
http://www.iana.org/assignments/media-types/media-types.xhtml
Please also check this amazing solution in below link (section "Using a static MIME Type Map"):
https://www.ryadel.com/en/get-file-content-mime-type-from-extension-asp-net-mvc-core/
You can simply use it like that:
var mineType=
MimeTypeMap.GetMimeType(System.IO.Path.GetExtension(submitted));
https://www.nuget.org/packages/MimeTypesMap/ is a great NuGet package for this. Use it like this:
MimeTypesMap.GetMimeType("filename.jpeg"); // => image/jpeg
or the other way:
MimeTypesMap.GetExtension("image/jpeg"); // => jpeg

Resources