当前位置: 编程技术>.net/c#/asp.net
本页文章导读:
▪ASP.NET的URL过滤 最近做的一个Web项目需要对URL进行过滤,在网上搜了一下,知道J2EE有个Filter的东西,而在.NET方面,其实也可以实现。下面是类的定义。 1 using System; 2 using System.Web; 3 using System.Web.SessionState; 4 .........
▪C#在托盘显示图标 ......
▪学用MVC4做网站五:5.1添加文章 五、文章在【ArticleController】添加[UserAdd()]action/// <summary> /// 添加文章 /// </summary> [UserAuthorize] public ActionResult UserAdd() { return View(new Article() { CommonModel.........
[1]ASP.NET的URL过滤
最近做的一个Web项目需要对URL进行过滤,在网上搜了一下,知道J2EE有个Filter的东西,而在.NET方面,其实也可以实现。
下面是类的定义。
1 using System;
2 using System.Web;
3 using System.Web.SessionState;
4
5 namespace QTJZ
6 {
7 public class Filters : IHttpModule, IRequiresSessionState
8 {
9 public void Dispose() { }
10
11 public void Init(HttpApplication application)
12 {
13 application.AcquireRequestState += new EventHandler(application_AcquireRequestState);
14 }
15
16 public void application_AcquireRequestState(object sender, EventArgs e)
17 {
18 HttpApplication application = sender as HttpApplication;
19 HttpRequest request = application.Request;
20 HttpResponse response = application.Response;
21
22 string url=request.CurrentExecutionFilePath.Trim('/');
23 string suffix = request.CurrentExecutionFilePathExtension.Trim('.');
24
25 if (!url.Equals("Default.htm") && (suffix == "aspx" || suffix == "htm"))
26 {
27 object sessionObj = application.Context.Session == null ? null : application.Session["useID"];
28 if (sessionObj==null)
29 {
30 response.Redirect("~/Default.htm");
31 }
32 }
33 }
34 }
35 }
2 using System.Web;
3 using System.Web.SessionState;
4
5 namespace QTJZ
6 {
7 public class Filters : IHttpModule, IRequiresSessionState
8 {
9 public void Dispose() { }
10
11 public void Init(HttpApplication application)
12 {
13 application.AcquireRequestState += new EventHandler(application_AcquireRequestState);
14 }
15
16 public void application_AcquireRequestState(object sender, EventArgs e)
17 {
18 HttpApplication application = sender as HttpApplication;
19 HttpRequest request = application.Request;
20 HttpResponse response = application.Response;
21
22 string url=request.CurrentExecutionFilePath.Trim('/');
23 string suffix = request.CurrentExecutionFilePathExtension.Trim('.');
24
25 if (!url.Equals("Default.htm") && (suffix == "aspx" || suffix == "htm"))
26 {
27 object sessionObj = application.Context.Session == null ? null : application.Session["useID"];
28 if (sessionObj==null)
29 {
30 response.Redirect("~/Default.htm");
31 }
32 }
33 }
34 }
35 }
为了实现过滤的效果,Filters类需要实现IHttpMoeld接口,实现该接口需要有两个方法,一个是Dispose,另一个是Init。Init的参数是一个HttpApplication类型的实例,利用这个实例注册一些事件。由于现在要对URL进行过滤,因此注册的是AcquireRequestState事件。类似事件罗列如下
BeginRequest在 ASP.NET 响应请求时作为 HTTP 执行管线链中的第一个事件发生。AuthenticateRequest当安全模块已建立用户标识时发生。AuthorizeRequest当安全模块已验证用户授权时发生。ResolveRequestCache在 ASP.NET 完成授权事件以使缓存模块从缓存中为请求提供服务后发生,
从而绕过事件处理程序(例如某个页或 XML Web services)的执行。
AcquireRequestState当 ASP.NET 获取与当前请求关联的当前状态(如会话状态)时发生。PreRequestHandlerExecute恰好在 ASP.NET 开始执行事件处理程序(例如,某页或某个 XML Web services)前发生。PostRequestHandlerExecute在 ASP.NET 事件处理程序(例如,某页或某个 XML Web service)执行完毕时发生。ReleaseRequestState在 ASP.NET 执行完所有请求事件处理程序后发生。 该事件将使状态模块保存当前状态数据。UpdateRequestCache当 ASP.NET 执行完事件处理程序以使缓存模块存储将用于从缓存为后续请求提供服务的响应时发生。EndRequest在 ASP.NET 响应请求时作为 HTTP 执行管线链中的最后一个事件发生。获取要跳转的url可以用request的CurrentExecutionFilePath属性,而获取请求文件的后缀可以用CurrentExecutionFile
[2]C#在托盘显示图标
C#在托盘显示图标using System;using System.Collections.Generic;using System.ComponentModel;using System.Data;using System.Drawing;using System.Linq;using System.Text;using System.Windows.Forms; namespace notifyIconShow{ public partial class notifyIconShow : Form { public notifyIconShow() { InitializeComponent(); } #region//设置鼠标放在托盘图标上面的文字 /// <summary> /// 方法名称:notifyIconShow_Load(窗体加载事件) /// 方法作用:设置鼠标放在托盘图标上面的文字 /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private void notifyIconShow_Load(object sender, EventArgs e) { //设置鼠标放在托盘图标上面的文字 this.notifyIcon1.Text = "这是托盘图标"; } #endregion #region//创建对象及声明变量 //创建NotifyIcon对象 NotifyIcon notifyicon = new NotifyIcon(); //创建托盘图标对象 Icon ico = new Icon("Clock2.ico"); //创建托盘菜单对象 ContextMenu notifyContextMenu = new ContextMenu(); #endregion #region//隐藏任务栏图标,显示托盘图标 /// <summary> /// 方法名称:notifyIconShow_SizeChanged(窗体大小改变后事件) /// 方法作用:隐藏任务栏图标,显示托盘图标 /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private void notifyIconShow_SizeChanged(object sender, EventArgs e) { //判断是否选择的是最小化按钮 if (WindowState == FormWindowState.Minimized) { //托盘显示图标等于托盘图标对象 //注意notifyIcon1是控件的名字而不是对象的名字 notifyIcon1.Icon = ico; //隐藏任务栏区图标 this.ShowInTaskbar = false; //图标显示在托盘区 notifyicon.Visible = true; } } #endregion #region//还原窗体 /// <summary> /// 方法名称:notifyIcon1_Click(托盘图标单击) /// 方法作用:还原窗体 /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private void notifyIcon1_Click(object sender, EventArgs e) { //判断是否已经最小化于托盘 if (WindowState == FormWindowState.Minimized) { //还原窗体显示 WindowState = FormWindowState.Normal; //激活窗体并给予它焦点 this.Activate(); //任务栏区显示图标 this.ShowInTaskbar = true; //托盘区图标隐藏 notifyicon.Visible = false; } } #endregion }}
本文链接
[3]学用MVC4做网站五:5.1添加文章
五、文章
在【ArticleController】添加[UserAdd()]action
/// <summary>
/// 添加文章
/// </summary>
[UserAuthorize]
public ActionResult UserAdd()
{
return View(new Article() { CommonModel = new CommonModel() });
}
/// 添加文章
/// </summary>
[UserAuthorize]
public ActionResult UserAdd()
{
return View(new Article() { CommonModel = new CommonModel() });
}
右键添加模型类为Article的强类型视图UserAdd.cshtml。编辑器选用KindEditor(国货当自强)……
@model Ninesky.Models.Article
@{
ViewBag.Title = "添加文章";
Layout = "~/Views/Shared/_User.cshtml";
}
<div >
<div >
<div >
<img alt="" src="/blog_article/~/Content/Default/User/Images/Icon/Article_16.png" />您现在的位置: 文章管理
</div>
<div>
@using (Html.BeginForm())
{
@Html.ValidationSummary(true)
<div >
<dl>
<dt>添加文章</dt>
<dd>
<div >
@Html.LabelFor(model => model.CommonModel.CategoryId) (必填) @Html.DisplayDescriptionFor(model => model.CommonModel.CategoryId)
</div>
<div >
@Html.HiddenFor(model => model.CommonModel.CategoryId)
<input type="text" class = "combotree" id="CommonModel_CategoryId_Text" />@Html.ValidationMessageFor(model => model.CommonModel.CategoryId)<br />
<ul id="categorytree" ><li>222</li></ul>
</div>
</dd>
<dd>
<div >
@Html.LabelFor(model => model.CommonModel.Title) (必填) @Html.DisplayDescriptionFor(model => model.CommonModel.Title)
</div>
<div >
@Html.EditorFor(model => model.CommonModel.Title)
@Html.ValidationMessageFor(model => model.CommonModel.Title)
</div>
</dd>
<dd>
<div >
@Html.LabelFor(model => model.Source)@Html.DisplayDescriptionFor(model => model.Source)
</div>
<div >
@Html.EditorFor(model => model.Source)
@Html.ValidationMessageFor(model => model.Source)
</div>
</dd>
<dd>
<div >
@Html.LabelFor(model => model.Intro)@Html.DisplayDescriptionFor(model => model.Intro)
</div>
<div >
@Html.TextAreaFor(model => model.Intro, new { @class = "Intro" })
@Html.ValidationMessageFor(model => model.Intro)
</div>
</dd>
<dd>
<div >
@Html.LabelFor(model => model.CommonModel.PicUrl)@Html.DisplayDescriptionFor(model => model.CommonModel.PicUrl)
</div>
<div >
@Html.EditorFor(model => model.CommonModel.PicUrl)
@Html.ValidationMessageFor(model => model.CommonModel.PicUrl)
</div>
</dd>
<dd>
@{
ViewBag.Title = "添加文章";
Layout = "~/Views/Shared/_User.cshtml";
}
<div >
<div >
<div >
<img alt="" src="/blog_article/~/Content/Default/User/Images/Icon/Article_16.png" />您现在的位置: 文章管理
</div>
<div>
@using (Html.BeginForm())
{
@Html.ValidationSummary(true)
<div >
<dl>
<dt>添加文章</dt>
<dd>
<div >
@Html.LabelFor(model => model.CommonModel.CategoryId) (必填) @Html.DisplayDescriptionFor(model => model.CommonModel.CategoryId)
</div>
<div >
@Html.HiddenFor(model => model.CommonModel.CategoryId)
<input type="text" class = "combotree" id="CommonModel_CategoryId_Text" />@Html.ValidationMessageFor(model => model.CommonModel.CategoryId)<br />
<ul id="categorytree" ><li>222</li></ul>
</div>
</dd>
<dd>
<div >
@Html.LabelFor(model => model.CommonModel.Title) (必填) @Html.DisplayDescriptionFor(model => model.CommonModel.Title)
</div>
<div >
@Html.EditorFor(model => model.CommonModel.Title)
@Html.ValidationMessageFor(model => model.CommonModel.Title)
</div>
</dd>
<dd>
<div >
@Html.LabelFor(model => model.Source)@Html.DisplayDescriptionFor(model => model.Source)
</div>
<div >
@Html.EditorFor(model => model.Source)
@Html.ValidationMessageFor(model => model.Source)
</div>
</dd>
<dd>
<div >
@Html.LabelFor(model => model.Intro)@Html.DisplayDescriptionFor(model => model.Intro)
</div>
<div >
@Html.TextAreaFor(model => model.Intro, new { @class = "Intro" })
@Html.ValidationMessageFor(model => model.Intro)
</div>
</dd>
<dd>
<div >
@Html.LabelFor(model => model.CommonModel.PicUrl)@Html.DisplayDescriptionFor(model => model.CommonModel.PicUrl)
</div>
<div >
@Html.EditorFor(model => model.CommonModel.PicUrl)
@Html.ValidationMessageFor(model => model.CommonModel.PicUrl)
</div>
</dd>
<dd>
最新技术文章:
 
站内导航:
特别声明:169IT网站部分信息来自互联网,如果侵犯您的权利,请及时告知,本站将立即删除!