我必须在将Content-Type标头值传递给HTTP请求之前验证它.
是否有针对所有可能值的特定列表Content-Type?
否则,有没有办法在HTTP请求中使用它之前验证内容类型?
我刚刚为我的一个类中的"application/json"内容类型声明了一个常量.
public const string JsonContentType = "application/json";
Run Code Online (Sandbox Code Playgroud)
我不确定这是一个好习惯.
.NET框架是否具有"application/json"的预定义const?
我能找到的最接近的东西是,System.Net.Mime.MediaTypeNames但似乎没有一切(如json),因为它似乎更专注于电子邮件附件.
我有一个现有的Web API 2服务,需要修改其中一个方法以将自定义对象作为另一个参数,目前该方法有一个参数,它是来自URL的简单字符串.将自定义对象添加为参数后,从.NET Windows应用程序调用服务时,我现在收到415不支持的媒体类型错误.有趣的是,我可以使用javascript和jquery ajax方法成功调用此方法.
Web API 2服务方法如下所示:
<HttpPost>
<HttpGet>
<Route("{view}")>
Public Function GetResultsWithView(view As String, pPaging As Paging) As HttpResponseMessage
Dim resp As New HttpResponseMessage
Dim lstrFetchXml As String = String.Empty
Dim lstrResults As String = String.Empty
Try
'... do some work here to generate xml string for the response
'// write xml results to response
resp.Content = New StringContent(lstrResults)
resp.Content.Headers.ContentType.MediaType = "text/xml"
resp.Headers.Add("Status-Message", "Query executed successfully")
resp.StatusCode = HttpStatusCode.OK
Catch ex As Exception
resp.StatusCode = HttpStatusCode.InternalServerError
resp.Headers.Add("Status-Message", String.Format("Error …Run Code Online (Sandbox Code Playgroud) vb.net json asp.net-web-api http-status-code-415 asp.net-web-api2
在以前的版本中,可以使用类似下面的代码在Web.Config文件中添加和调整所有这些设置:
<staticContent>
<mimeMap fileExtension=".webp" mimeType="image/webp" />
<!-- Caching -->
<clientCache cacheControlMode="UseMaxAge" cacheControlMaxAge="96:00:00" />
</staticContent>
<httpCompression directory="%SystemDrive%\inetpub\temp\IIS Temporary Compressed Files">
<scheme name="gzip" dll="%Windir%\system32\inetsrv\gzip.dll" />
<dynamicTypes>
<add mimeType="text/*" enabled="true" />
<add mimeType="message/*" enabled="true" />
<add mimeType="application/javascript" enabled="true" />
<add mimeType="*/*" enabled="false" />
</dynamicTypes>
<staticTypes>
<add mimeType="text/*" enabled="true" />
<add mimeType="message/*" enabled="true" />
<add mimeType="application/javascript" enabled="true" />
<add mimeType="*/*" enabled="false" />
</staticTypes>
</httpCompression>
<urlCompression doStaticCompression="true" doDynamicCompression="true"/>
Run Code Online (Sandbox Code Playgroud)
但是,由于Web.Config不再出现在ASP.NET vNext中,你如何调整这样的设置?我搜索过网络和ASP.NET Github仓库,但没有遇到任何问题 - 任何想法?
我想在我的代码中保存Mime类型.现在我用来做这个:
string mYMimeType = "text/plain";
Run Code Online (Sandbox Code Playgroud)
有没有办法将它保存在(已经存在的)标准专用类中?就像是...
Http.MimeTypes myMimeType = Http.MimeTypes.TextPlain;
Run Code Online (Sandbox Code Playgroud) content-type ×3
.net ×2
asp.net-mvc ×2
c# ×2
mime-types ×2
asp.net ×1
asp.net-core ×1
caching ×1
compression ×1
http ×1
http-headers ×1
http-request ×1
json ×1
vb.net ×1