- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我有以下 C# 代码。我正在使用 ASP.NET MVC 3。
public override void ExecuteResult(ControllerContext context)
{
// If ContentType is not expected to be application/json, then return XML
if ((context.HttpContext.Request.ContentType ?? String.Empty).Contains("application/json"))
{
new JsonResult { Data = this.Data }
.ExecuteResult(context);
}
else
{
using (MemoryStream stream = new MemoryStream(500))
{
using (var xmlWriter = XmlTextWriter.Create(
stream,
new XmlWriterSettings()
{
OmitXmlDeclaration = true,
Encoding = UTF8,
Indent = true
}))
{
new XmlSerializer(typeof(T), IncludedTypes)
.Serialize(xmlWriter, this.Data);
}
// NOTE: We need to cache XmlSerializer for specific type. Probably use the
// GenerateSerializer to generate compiled custom made serializer for specific
// types and then cache the reference
new ContentResult
{
ContentType = "text/xml",
Content = UTF8.GetString(stream.ToArray()),
ContentEncoding = UTF8
}
.ExecuteResult(context);
}
}
}
我正在尝试根据请求返回 json 或 xml 结果。问题是我运行时得到 context.HttpContext.Request.ContentType = ""
。
有没有办法让应用程序知道请求是“application/json”?
我在名为 GetGoogleMapsMarkers 的 Controller 方法中返回此结果对象:
$(document).ready(function () {
$.ajax({
type: "POST",
url: "http://localhost:1939/API/Google/GetGoogleMapsMarkers",
datatype: "json",
contentType: "application/json; charset=utf-8",
success: function (data) {
alert(data);
}
}
});
请帮帮我。谢谢。
最佳答案
无法重现。这是我尝试过的:
结果:
public class TestResult : ActionResult
{
public override void ExecuteResult(ControllerContext context)
{
var ct = context.HttpContext.Request.ContentType;
context.HttpContext.Response.Write(ct);
}
}
Controller :
public class HomeController : Controller
{
public ActionResult Index()
{
return View();
}
public ActionResult Foo()
{
return new TestResult();
}
}
查看:
<script type="text/javascript">
$.ajax({
url: '@Url.Action("foo")',
type: 'POST',
contentType: 'application/json; charset=utf-8',
success: function (result) {
alert(result);
}
});
</script>
AJAX 调用导致获取正确的请求内容类型。
关于asp.net - 在 ASP.NET MVC 3 中获取 ContentType,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5644715/
我正在尝试将一些模型从一个 Django 应用程序迁移到另一个应用程序,并基于这个问题 How do I migrate a model out of one django app and into
我正在开发我的 android 项目,我正在创建一个请求函数来上传文件,该请求如下所示, httpClient.put(uri) { body = MultiPartFormDataConte
我正在尝试学习如何使用 ContentTypes 框架,我似乎无法找到我自己的应用程序。 文档 have clear instructions用于从 django.contrib.sites 导入模型
我最近尝试将一些装置加载到我的数据库中。当我运行服务器并加载各种页面时,虽然出现错误: Caught DoesNotExist while rendering: ContentType matchin
我用 async await 写了一些简单的代码,但是当我尝试运行它时,编译器抛出一个 System.InvalidOperationException。 完整的错误信息是: Unhandled Ex
微软有篇文章:http://support.microsoft.com/kb/193998/zh-cn,用doc读取和显示二进制数据,《怎样读取二进制文件》,没测试,懒得编译dll &nb
本文整理了Java中okreplay.YamlRecordedResponse.contentType()方法的一些代码示例,展示了YamlRecordedResponse.contentType()
写在前边 最近在写 OAuth2 对接的代码,由于授权服务器(竹云BambooCloud IAM)部署在甲方内网,所以想着自己 Mock 一下授权方的返回体,验证一下我的代码。我这才踩到了坑…… 故事
我的数据迁移文件中有这个: def set_target_user(apps, schema_editor): LogEntry = apps.get_model('auditlog', 'L
如何在 Django 单元测试中使用 GenericRelations? 我在互联网上阅读并尝试了无数建议,但无济于事。 这个很有前途Problems with contenttypes when l
我正在设计一个 api,它返回一个从第三方服务获取的图像 url。 我的问题是,对于所有媒体,我都使用我所谓的“媒体对象” { "ConetetType": "image/jpeg", "href
我有这些模型: class App(models.Model): name = models.CharField(max_length=100) class ProjectA(models.Mod
什么时候创建新的 ContentType?我如何自己触发它? 我进行了大量迁移、创建新模型等。完成所有操作后,将进行最终的数据迁移,将这些新对象的权限分配给已拥有相关权限的任何组。 (特此“相关权限”
在我们基于 ASP.NET Core 的 Web 应用程序中,我们需要以下内容:某些请求的文件类型应获得自定义 ContentType 的响应。例如。 .map应映射到 application/jso
我正在使用 Apache 和 mod_wsgi 将 Django 1.10 项目部署到 Ubuntu 服务器上。我收到以下无法解决的 500 错误: RuntimeError: Model class
我有两个表,Content 和 ContentType 每个内容都有一种内容类型 每种内容类型可能包含在许多内容中。 这是我的模型: @Entity @Table(name = "content")
我正在尝试使以下内容适合 Django ORM。有一个 Publish 模型来管理不同类型内容的发布(其他模型)。这样我就可以轻松地执行 Publish.objects.all() 并按日期排序。我做
我想从 ContentType 实例中获取对象的查询集,然后能够过滤它们。从文档中,可以到 get()一个对象使用: ct.get_object_for_this_type(**kwargs) 如何制
假设这是用户照片的模型: class Photo(models.Model): user = models.ForeignKey(User) title = models.CharFi
This web page有一个 ASP.NET MVC 代码示例,其中包括以下行: [Route("sitemap.xml")] public ActionResult SitemapXml() {
我是一名优秀的程序员,十分优秀!