更改默认的ASP MVC请求头添加自己的价值观 [英] Change default ASP MVC Request Header to add your own values

查看:129
本文介绍了更改默认的ASP MVC请求头添加自己的价值观的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试着改变我所有的ASP MVC HTTP响应头有默认的另一个值在我的博客应用程序中实现pingback的自动发现。

Im trying to change all my ASP MVC HTTP response headers to have another value by default for implementing Pingback auto-discovery in my blog application.

的缺省头(上卡西尼)是:

The default header (on Cassini) is :

Cache-Control   private
Connection  Close
Content-Length  20901
Content-Type    text/html; charset=utf-8
Date    Fri, 20 Apr 2012 22:46:11 GMT
Server  ASP.NET Development Server/10.0.0.0
X-AspNet-Version    4.0.30319
X-AspNetMvc-Version 3.0

和我想增加一个额外的值:

and i want an extra value added :

X-Pingback: http://localhost:4912/pingback/xmlrpcserver

我用Google搜索了一下,发现一个NEET的解决方案:
- 从 ActionFilterAttribute 派生并重写 OnResultExecuted 方式:

public class HttpHeaderAttribute : ActionFilterAttribute
    {

        public string Name { get; set; }
        public string Value { get; set; }

        public HttpHeaderAttribute(string name, string value)
        {
            Name = name;
            Value = value;
        }

        public override void OnResultExecuted(ResultExecutedContext filterContext)
        {
            filterContext.HttpContext.Request.Headers.Add(Name, Value);
            base.OnResultExecuted(filterContext);
        }

    }

和后来干脆我把属性我控制器的方法:

And then simply i put the attribute on my Controllers methods:

[HttpHeader("X-Pingback","http://localhost:4912/pingback/xmlrpcserver")]
        public ActionResult Index()
        {
            var allArticles = _repository.GetPublishedArticles(SortOrder.desc);
            return View(allArticles);
        }

当我矮的应用程序,我得到以下错误:

When i runt the app i get the following error :

任何想法?

推荐答案

这可能会或可能无法正常工作(很明显,如果没​​有,我会删除用户未来的答案)。这听起来像从异常操作不受此平台支持,即卡西尼许多不支持自定义页眉(这是很奇怪的,但可能性)。我的建议是要确保您使用的是的Visual Studio 2010 SP1 ,然后再安装 IIS防爆preSS (其中是升级到卡西尼,更加像真正的IIS多),然后切换项目中使用IIS防爆preSS,看看你是否得到相同的异常。

This may or may not work (obviously if it doesn't I'll delete the answer for future users). It sounds like from the exception Operation is not supported by this platform, that Cassini many not support custom headers (which would be quite strange, but a possibility). What I would suggest is to make sure you are using Visual Studio 2010 SP1, then install IIS Express (which is an upgrade to Cassini and is much more like real IIS), and then switch your project to use IIS Express and see if you get the same exception.

此外,您可以查看<一个href=\"http://stackoverflow.com/questions/32824/why-does-httpcacheability-private-sup$p$pss-etags\">Why确实HttpCacheability.Private燮preSS的ETag?因为它也可能会给你一个替代解决方案。

Additionally, you may want to review Why does HttpCacheability.Private suppress ETags? as it may also give you an alternative solution.

这篇关于更改默认的ASP MVC请求头添加自己的价值观的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆