简体   繁体   English

Umbraco-检查请求是否为XmlHttpRequest

[英]Umbraco - Check if request is XmlHttpRequest

I'm doing an AJAX feature for my Umbraco website. 我正在为Umbraco网站进行AJAX功能。

Basically what I'm trying to do is when someone requests for http://mysite.com/news with the XmlHttpRequest header then I want Umbraco to return JSON of the following page. 基本上,我想做的是有人请求带有XmlHttpRequest标头的http://mysite.com/news时 ,我希望Umbraco返回下一页的JSON。

What I've been trying to do is finding the right place to check for the header in Umbraco. 我一直试图做的是找到合适的位置来检查Umbraco中的标题。 Basically I want to override or inherit some class where I could check the header and return the page in Json or HTML depending on the header. 基本上,我想重写或继承一些可以检查标头并根据标头以Json或HTML返回页面的类。

I'm using Umbraco 4.7.0 (1.0.4090.21631) 我正在使用Umbraco 4.7.0(1.0.4090.21631)

Any feedback would be more than welcome! 任何反馈都将非常受欢迎! Thanks! 谢谢!

You should check the Content-Type header from server side logic. 您应该从服务器端逻辑检查Content-Type标头。 Your Javascript should be specifying a Content-Type like application/json . 您的Javascript应该指定一个Content-Type例如application/json

I'd probably use the URL Rewriting module for IIS7 to detect the content-type in the request, then redirect to the same URL with /jsonTemplate on the end of it. 我可能会使用IIS7的URL重写模块来检测请求中的内容类型,然后使用末尾的/ jsonTemplate重定向到相同的URL。 You would then create a masterpage called jsonTemplate, with a single macro inside it, which would return your JSON. 然后,您将创建一个名为jsonTemplate的母版,其中包含单个宏,该母版将返回JSON。

This is common for Umbraco - specifying a separate template with a separate macro to return a different view of the same content. 这对于Umbraco来说很常见-使用单独的宏指定单独的模板以返回相同内容的不同视图。 Check out http://blog4umbraco.codeplex.com/SourceControl/changeset/view/68424#1138733 for an example of an XSLT macro that returns RSS using this approach. 请查看http://blog4umbraco.codeplex.com/SourceControl/changeset/view/68424#1138733 ,以获取使用此方法返回RSS的XSLT宏示例。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM