ASP.NET MVC Action Filters中有趣的Http Headers
2009-05-02 21:40
501 查看
.post {
padding: 0;
margin: 0;
}
.posthead {
margin: 0;
padding: 1em 0 0.5em;
text-align: right;
font-size: 0.9em;
}
.posthead h2
{
color: #0000CC;
font-family: Verdana, Arial, Helvetica, sans-serif;
font-size: 1.1em;
}
.posthead h2 {
font-size: 1.1em;
margin: 0;
padding: 0.5em 0;
letter-spacing: 1px;
text-transform: uppercase;
text-align: left;
}
h2 {
margin-top: 1em;
font-size: 1em;
}
p {
margin: 1em 0;
}
.dp-highlighter
{
font-family: "Consolas", "Courier New", Courier, mono, serif;
font-size: 12px;
background-color: #E7E5DC;
width: 99%;
overflow: auto;
margin: 18px 0 18px 0 !important;
padding-top: 1px; /* adds a little border on top when controls are hidden */
}
.dp-highlighter .bar
{
padding-left: 45px;
}
.dp-highlighter .tools
{
padding: 3px 8px 3px 10px;
font: 9px Verdana, Geneva, Arial, Helvetica, sans-serif;
color: silver;
background-color: #f8f8f8;
padding-bottom: 10px;
border-left: 3px solid #6CE26C;
}
.dp-highlighter .tools a
{
font-size: 9px;
color: #a0a0a0;
background-color: inherit;
text-decoration: none;
margin-right: 10px;
}
.dp-highlighter a,
.dp-highlighter a:hover
{
background: none;
border: none;
padding: 0;
margin: 0;
}
.dp-highlighter ol
{
list-style: decimal; /* for ie */
background-color: #fff;
margin: 0px 0px 1px 45px !important; /* 1px bottom margin seems to fix occasional Firefox scrolling */
padding: 0px;
color: #5C5C5C;
}
.dp-highlighter ol,
.dp-highlighter ol li,
.dp-highlighter ol li span
{
margin: 0;
padding: 0;
border: none;
}
.dp-highlighter ol li.alt
{
background-color: #FFF;
color: inherit;
}
.dp-highlighter ol li,
.dp-highlighter .columns div
{
list-style: decimal-leading-zero; /* better look for others, override cascade from OL */
list-style-position: outside !important;
border-left: 3px solid #6CE26C;
background-color: #F8F8F8;
color: #5C5C5C;
padding: 0 3px 0 10px !important;
margin: 0 !important;
line-height: 14px;
}
.dp-highlighter ol li span
{
color: black;
background-color: inherit;
}
.dp-highlighter .string { color: blue; background-color: inherit; }
.dp-highlighter .keyword { color: #069; font-weight: bold; background-color: inherit; }
.dp-highlighter .comment, .dp-highlighter .comments { color: #008200; background-color: inherit; }
.comment
{
padding: 4px;
}
.comment
{
padding: 4px;
overflow: hidden;
word-wrap: break-word; /* Internet Explorer 5.5+ */
}
应用程序在一段时间后自动重定向一个指定的URL,是的你能用javascript中window.location来实现,除了javascript外呢?看这个代码:
[AutoRefresh(ControllerName = "Home", ActionName = "About", DurationInSeconds = 10)]
public ActionResult Index1()
[AutoRefresh(ActionName = "About", DurationInSeconds = 15)]
public ActionResult Index2()
[AutoRefresh(RouteName = "ByFavoriteRoute", DurationInSeconds = 30)]
public ActionResult Index3()
[AutoRefresh(DurationInSeconds = 45)]
public ActionResult Index4()
如果浏览器在特定期间是闲置的,它将自动重定向到那个Action.如果你不指定任何action/controller/route,它将自动重定向当前URL.怎样?好的我只是使用/滥用一些http
header,来看下面:
[AttributeUsage(AttributeTargets.Class | AttributeTargets.Method, Inherited = true, AllowMultiple = false)]
[AspNetHostingPermission(SecurityAction.Demand, Level = AspNetHostingPermissionLevel.Minimal)]
public class AutoRefreshAttribute : ActionFilterAttribute
{
public const int DefaultDurationInSeconds = 300; // 5 Minutes
public AutoRefreshAttribute()
{
DurationInSeconds = DefaultDurationInSeconds;
}
public int DurationInSeconds
{
get;
set;
}
public string RouteName
{
get;
set;
}
public string ControllerName
{
get;
set;
}
public string ActionName
{
get;
set;
}
public override void OnResultExecuted(ResultExecutedContext filterContext)
{
string url = BuildUrl(filterContext);
string headerValue = string.Concat(DurationInSeconds, ";Url=", url);
filterContext.HttpContext.Response.AppendHeader("Refresh", headerValue);
base.OnResultExecuted(filterContext);
}
private string BuildUrl(ControllerContext filterContext)
{
UrlHelper urlHelper = new UrlHelper(filterContext.RequestContext);
string url;
if (!string.IsNullOrEmpty(RouteName))
{
url = urlHelper.RouteUrl(RouteName);
}
else if (!string.IsNullOrEmpty(ControllerName) && !string.IsNullOrEmpty(ActionName))
{
url = urlHelper.Action(ActionName, ControllerName);
}
else if (!string.IsNullOrEmpty(ActionName))
{
url = urlHelper.Action(ActionName);
}
else
{
url = filterContext.HttpContext.Request.RawUrl;
}
return url;
}
}
不能想像适当使用这个action filter,可能你能在你你要显示一些在线统计之类页面任何iframe中使用它.好的,现在让我们试试滥用这个Refeash
header,这时它是一个相当通的场景.
你要安全的使用(标记为已认证)action,当会话过期时自动重定向到登陆页面.现试试这个:
[AttributeUsage(AttributeTargets.Class | AttributeTargets.Method, Inherited = true, AllowMultiple = false)]
[AspNetHostingPermission(SecurityAction.Demand, Level = AspNetHostingPermissionLevel.Minimal)]
public class AutoRedirecToLogin : ActionFilterAttribute
{
public override void OnResultExecuted(ResultExecutedContext filterContext)
{
string url = FormsAuthentication.LoginUrl;
int durationInSeconds = ((filterContext.HttpContext.Session.Timeout * 60) + 10); // Extra 10 seconds
string headerValue = string.Concat(durationInSeconds, ";Url=", url);
filterContext.HttpContext.Response.AppendHeader("Refresh", headerValue);
base.OnResultExecuted(filterContext);
}
}
当会话过期时,不需要额外的用户点击它将自动重定向到登陆页面.
警告:如果你要你的URL被搜索引擎收录为索引,那以不要使用Refresh meta
头(验证过Google,其它的不确定).例如,你的页面A要自动重定向页面B,那么页面A只有这个头,在那种情况下页面A将不会被索引.
Roni Schuetz 刚通知他有一个聚集 mvc action filters的项目在CodePlex
上, 对于他的项目这两个应该是最佳候选.
Author: Petter Liu http://wintersun.cnblogs.com
Source
padding: 0;
margin: 0;
}
.posthead {
margin: 0;
padding: 1em 0 0.5em;
text-align: right;
font-size: 0.9em;
}
.posthead h2
{
color: #0000CC;
font-family: Verdana, Arial, Helvetica, sans-serif;
font-size: 1.1em;
}
.posthead h2 {
font-size: 1.1em;
margin: 0;
padding: 0.5em 0;
letter-spacing: 1px;
text-transform: uppercase;
text-align: left;
}
h2 {
margin-top: 1em;
font-size: 1em;
}
p {
margin: 1em 0;
}
.dp-highlighter
{
font-family: "Consolas", "Courier New", Courier, mono, serif;
font-size: 12px;
background-color: #E7E5DC;
width: 99%;
overflow: auto;
margin: 18px 0 18px 0 !important;
padding-top: 1px; /* adds a little border on top when controls are hidden */
}
.dp-highlighter .bar
{
padding-left: 45px;
}
.dp-highlighter .tools
{
padding: 3px 8px 3px 10px;
font: 9px Verdana, Geneva, Arial, Helvetica, sans-serif;
color: silver;
background-color: #f8f8f8;
padding-bottom: 10px;
border-left: 3px solid #6CE26C;
}
.dp-highlighter .tools a
{
font-size: 9px;
color: #a0a0a0;
background-color: inherit;
text-decoration: none;
margin-right: 10px;
}
.dp-highlighter a,
.dp-highlighter a:hover
{
background: none;
border: none;
padding: 0;
margin: 0;
}
.dp-highlighter ol
{
list-style: decimal; /* for ie */
background-color: #fff;
margin: 0px 0px 1px 45px !important; /* 1px bottom margin seems to fix occasional Firefox scrolling */
padding: 0px;
color: #5C5C5C;
}
.dp-highlighter ol,
.dp-highlighter ol li,
.dp-highlighter ol li span
{
margin: 0;
padding: 0;
border: none;
}
.dp-highlighter ol li.alt
{
background-color: #FFF;
color: inherit;
}
.dp-highlighter ol li,
.dp-highlighter .columns div
{
list-style: decimal-leading-zero; /* better look for others, override cascade from OL */
list-style-position: outside !important;
border-left: 3px solid #6CE26C;
background-color: #F8F8F8;
color: #5C5C5C;
padding: 0 3px 0 10px !important;
margin: 0 !important;
line-height: 14px;
}
.dp-highlighter ol li span
{
color: black;
background-color: inherit;
}
.dp-highlighter .string { color: blue; background-color: inherit; }
.dp-highlighter .keyword { color: #069; font-weight: bold; background-color: inherit; }
.dp-highlighter .comment, .dp-highlighter .comments { color: #008200; background-color: inherit; }
.comment
{
padding: 4px;
}
.comment
{
padding: 4px;
overflow: hidden;
word-wrap: break-word; /* Internet Explorer 5.5+ */
}
ASP.NET MVC Action Filters中有趣的Http Headers
你想要你的Asp.net mvc应用程序在一段时间后自动重定向一个指定的URL,是的你能用javascript中window.location来实现,除了javascript外呢?看这个代码:
[AutoRefresh(ControllerName = "Home", ActionName = "About", DurationInSeconds = 10)]
public ActionResult Index1()
[AutoRefresh(ActionName = "About", DurationInSeconds = 15)]
public ActionResult Index2()
[AutoRefresh(RouteName = "ByFavoriteRoute", DurationInSeconds = 30)]
public ActionResult Index3()
[AutoRefresh(DurationInSeconds = 45)]
public ActionResult Index4()
[AutoRefresh(ControllerName = "Home", ActionName = "About", DurationInSeconds = 10)] public ActionResult Index1() [AutoRefresh(ActionName = "About", DurationInSeconds = 15)] public ActionResult Index2() [AutoRefresh(RouteName = "ByFavoriteRoute", DurationInSeconds = 30)] public ActionResult Index3() [AutoRefresh(DurationInSeconds = 45)] public ActionResult Index4()
如果浏览器在特定期间是闲置的,它将自动重定向到那个Action.如果你不指定任何action/controller/route,它将自动重定向当前URL.怎样?好的我只是使用/滥用一些http
header,来看下面:
[AttributeUsage(AttributeTargets.Class | AttributeTargets.Method, Inherited = true, AllowMultiple = false)]
[AspNetHostingPermission(SecurityAction.Demand, Level = AspNetHostingPermissionLevel.Minimal)]
public class AutoRefreshAttribute : ActionFilterAttribute
{
public const int DefaultDurationInSeconds = 300; // 5 Minutes
public AutoRefreshAttribute()
{
DurationInSeconds = DefaultDurationInSeconds;
}
public int DurationInSeconds
{
get;
set;
}
public string RouteName
{
get;
set;
}
public string ControllerName
{
get;
set;
}
public string ActionName
{
get;
set;
}
public override void OnResultExecuted(ResultExecutedContext filterContext)
{
string url = BuildUrl(filterContext);
string headerValue = string.Concat(DurationInSeconds, ";Url=", url);
filterContext.HttpContext.Response.AppendHeader("Refresh", headerValue);
base.OnResultExecuted(filterContext);
}
private string BuildUrl(ControllerContext filterContext)
{
UrlHelper urlHelper = new UrlHelper(filterContext.RequestContext);
string url;
if (!string.IsNullOrEmpty(RouteName))
{
url = urlHelper.RouteUrl(RouteName);
}
else if (!string.IsNullOrEmpty(ControllerName) && !string.IsNullOrEmpty(ActionName))
{
url = urlHelper.Action(ActionName, ControllerName);
}
else if (!string.IsNullOrEmpty(ActionName))
{
url = urlHelper.Action(ActionName);
}
else
{
url = filterContext.HttpContext.Request.RawUrl;
}
return url;
}
}
[AttributeUsage(AttributeTargets.Class | AttributeTargets.Method, Inherited = true, AllowMultiple = false)] [AspNetHostingPermission(SecurityAction.Demand, Level = AspNetHostingPermissionLevel.Minimal)] public class AutoRefreshAttribute : ActionFilterAttribute { public const int DefaultDurationInSeconds = 300; // 5 Minutes public AutoRefreshAttribute() { DurationInSeconds = DefaultDurationInSeconds; } public int DurationInSeconds { get; set; } public string RouteName { get; set; } public string ControllerName { get; set; } public string ActionName { get; set; } public override void OnResultExecuted(ResultExecutedContext filterContext) { string url = BuildUrl(filterContext); string headerValue = string.Concat(DurationInSeconds, ";Url=", url); filterContext.HttpContext.Response.AppendHeader("Refresh", headerValue); base.OnResultExecuted(filterContext); } private string BuildUrl(ControllerContext filterContext) { UrlHelper urlHelper = new UrlHelper(filterContext.RequestContext); string url; if (!string.IsNullOrEmpty(RouteName)) { url = urlHelper.RouteUrl(RouteName); } else if (!string.IsNullOrEmpty(ControllerName) && !string.IsNullOrEmpty(ActionName)) { url = urlHelper.Action(ActionName, ControllerName); } else if (!string.IsNullOrEmpty(ActionName)) { url = urlHelper.Action(ActionName); } else { url = filterContext.HttpContext.Request.RawUrl; } return url; } }
不能想像适当使用这个action filter,可能你能在你你要显示一些在线统计之类页面任何iframe中使用它.好的,现在让我们试试滥用这个Refeash
header,这时它是一个相当通的场景.
你要安全的使用(标记为已认证)action,当会话过期时自动重定向到登陆页面.现试试这个:
[AttributeUsage(AttributeTargets.Class | AttributeTargets.Method, Inherited = true, AllowMultiple = false)]
[AspNetHostingPermission(SecurityAction.Demand, Level = AspNetHostingPermissionLevel.Minimal)]
public class AutoRedirecToLogin : ActionFilterAttribute
{
public override void OnResultExecuted(ResultExecutedContext filterContext)
{
string url = FormsAuthentication.LoginUrl;
int durationInSeconds = ((filterContext.HttpContext.Session.Timeout * 60) + 10); // Extra 10 seconds
string headerValue = string.Concat(durationInSeconds, ";Url=", url);
filterContext.HttpContext.Response.AppendHeader("Refresh", headerValue);
base.OnResultExecuted(filterContext);
}
}
[AttributeUsage(AttributeTargets.Class | AttributeTargets.Method, Inherited = true, AllowMultiple = false)] [AspNetHostingPermission(SecurityAction.Demand, Level = AspNetHostingPermissionLevel.Minimal)] public class AutoRedirecToLogin : ActionFilterAttribute { public override void OnResultExecuted(ResultExecutedContext filterContext) { string url = FormsAuthentication.LoginUrl; int durationInSeconds = ((filterContext.HttpContext.Session.Timeout * 60) + 10); // Extra 10 seconds string headerValue = string.Concat(durationInSeconds, ";Url=", url); filterContext.HttpContext.Response.AppendHeader("Refresh", headerValue); base.OnResultExecuted(filterContext); } }
当会话过期时,不需要额外的用户点击它将自动重定向到登陆页面.
警告:如果你要你的URL被搜索引擎收录为索引,那以不要使用Refresh meta
头(验证过Google,其它的不确定).例如,你的页面A要自动重定向页面B,那么页面A只有这个头,在那种情况下页面A将不会被索引.
Roni Schuetz 刚通知他有一个聚集 mvc action filters的项目在CodePlex
上, 对于他的项目这两个应该是最佳候选.
Author: Petter Liu http://wintersun.cnblogs.com
Source
相关文章推荐
- HTTP Modules versus ASP.NET MVC Action Filters
- ASP.NET MVC Action Filters
- ASP.NET MVC Framework Action Filters
- ASP.NET MVC Action Filters
- 了解ASP.NET MVC几种ActionResult的本质:HttpStatusCodeResult & RedirectResult/RedirectToRouteResult
- 了解ASP.NET MVC几种ActionResult的本质:HttpStatusCodeResult & RedirectResult/RedirectToRouteResult
- ASP.NET MVC 3–Global Action Filters
- Action Filters for ASP.NET MVC
- ASP.NET MVC Action Filters
- 理解ASP.NET MVC Framework Action Filters
- ASP.NET MVC的Ajax.ActionLink 的HttpMethod="Get" 一个重复请求的BUG
- 了解ASP.NET MVC几种ActionResult的本质:HttpStatusCodeResult & RedirectResult/RedirectToRouteResult
- 理解ASP.NET MVC Framework Action Filters(翻的)
- Asp.Net MVC 1.0 Action Filters Tips
- 理解ASP.NET MVC Framework Action Filters(翻的)
- 了解ASP.NET MVC几种ActionResult的本质:HttpStatusCodeResult & RedirectResult/RedirectToRouteResult
- asp.net mvc下标记一个action同时可以接受httpget和httppost
- ASP.NET MVC ActionResult的其它返回值
- ASP.NET MVC 重点教程一周年版 第五回 ActionResult的其它返回值
- Asp.net MVC Action大全(转)