配置webconfig

记录下webconfig的配置(复制很久的笔记):

1、 配置Session在url传递

在web.config中的 <system.web> 节点下添加,手动修改session的相关设置

<sessionState timeout="5" cookieless="true"> </sessionState>

2、 配置上传文件大于4M,里面是字节数,比如下面10M是1024*1024*10

<system.web>

    <httpRuntime targetFramework="4.5" maxRequestLength="10485760"/>

</system.web>

3、比如上传1g的内容。请求的筛选模块是被直接拒绝,不会到后台指定的页面的,跟上面的界面配置要相同才行

<configuration>

  <system.webServer>

<security>

    <requestFiltering>

      <requestLimits maxAllowedContentLength="1073741824"></requestLimits>

    </requestFiltering>

  </security>

  </system.webServer>

</configuration>

4、自定义一般处理程序类及配置

①新建一个类,继承IHttpHandler,实现里面的ProcessRequest方法与IsReusable属性。属性中如果get{return false;}表示每次浏览器的请求都会新创建这个一般处理程序的类的对象,为true每次浏览器请求都是重用第一次创建的类的对象

②比如用户需要xxx.hh这样子在浏览器中访问,我们新建的这个类继承IhttpHandler接口之后实现里面的代码专门接管处理这样的需求。如果要要Session,还要实现Session的接口。 verb的GET与POST一定是大写,也可以*或者GET或者POST。

③参数name:可以由程序自己定义,建议取有意义的名字,多个配置的name值不能重复

      参数path:告诉asp.net处理机制,什么样的url才会给 type指定的类型类处理

      参数verb:告诉asp.net处理机制,是GET或者POST请求才进行截获处理

IIS集成模式下配置

 <system.webServer>

<!--适配IIS集成模式-->

    <handlers>

      <add name="iishander" path="*.hh" verb="*" type="WebApplication1.IISHandler1"/>

    </handlers>

  </system.webServer>

IIS经典模式下配置

<system.web>

 <httpHandlers>

      <add path="*.hh" verb="get,post" type="WebApplication1.IISHandler1" />

    </httpHandlers>

  </system.web>

5、配置URL重写,新建一个类实现IHttpMoudle接口,或者写在Global中(不需要配置文件)

①<system.webServer>

    <modules>

      <add name="url rewrite" type="利用过滤器实现url重写.UrlRewrite"/>

    </modules>

  </system.webServer>

②利用IIS  Url重写模块来实现url的友好重写

利用IIS  Url重写模块来实现url的友好重写

在Web平台安装程序下载URL重写工具安装,返回就看到这个工具了。点击打开,添加规则,友好url,输入动态的url……(就是重写之前的url)

6、进程外Session

使用状态服务器存session

注意session存储的对象必须支持可序列化,即类上面增加[Serializable]特性

<sessionState stateConnectionString="tcpip=127.0.0.1:42424" mode="StateServer"/>

修改默认端口:

使用数据库存session

1、  Session保存的位置:

<sessionState mode=“InProc | StateServer| SQLServer”>

<sessionState stateConnectionString="tcpip=127.0.0.1:42424" mode="StateServer"/>

创建数据库脚本文件在C:\Windows\Microsoft.NET\Framework\v4.0.30319下的:InstallPersistSqlState.sql和InstallSqlState.sql

可以用下面命令安装保存Session的数据库,sa,master aspbetdb用户名,密码,数据库

aspnet_regsql -U sa -P master –ssadd -sstype c -d aspnetdb

一定要开数据库的TCPIP连接,否则不能创建成功

web.config可以配置成:

<sessionState  sqlConnectionString="server=.;database=aspnetdb;uid=sa;pwd=;" allowCustomSqlDatabase="true"  mode="SQLServer"/>

7、Cache

<%@ OutputCache Duration="60" VaryByParam="none" %>

VaryByParam

是指页面根据使用 POST 或 GET 发送的名称/值对(参数)来更新缓存的内容,多个参数用分号隔开。如果不希望根据任何参数来改变缓存内容,请将值设置为 none。如果希望通过所有的参数值改变都更新缓存,请将属性设置为星号 (*)。

例如: http://localhost:1852/WebForm.aspx?p=1 
则可以在WebForm1.aspx页面头部声明缓存:

<%@ OutputCache Duration="60" VaryByParam="p" %>

以上代码设置页面缓存时间是60秒,并根据p参数的值来更新缓存,即p的值发生变化才更新缓存。

如果一直是WebForm1.aspx?p=1访问该页,则页面会缓存当前数据,当p=2时又会执行后台代码更新缓存内容。

如果有多个参数时,如:http://localhost:1852/WebForm.aspx?pp=1&ln=1

可以这样声明:<%@ OutputCache Duration="60" VaryByParam="pp;ln" %> 

根据控件ID缓存<%@ OutputCache Duration="60" VaryByControl="ID" %> 

在WebConfig中配置,直接读取配置文件(指令集:<%@ OutputCache CacheProfile=”cache10”%>)

<system.web>

   <caching>

        <outputCacheSettings>

          <outputCacheProfiles>

            <add name="cache10" duration="10" varyByParam="none" />

          </outputCacheProfiles>

        </outputCacheSettings>

      </caching>

</system.web>

数据库缓存依赖:

数据库缓存依赖

实现步骤:

    下面就让我们看一下如何实现数据库缓存依赖功能:

第一步: 修改web.config,让项目启用SqlCacheDependency 。

将下列代码加入web.config的<system.web>节:       

<?xml version="1.0"?>

<configuration>

 <appSettings/>

<connectionStrings>

<add name="connectionstring" connectionString="data source=127.0.0.1;initial catalog=Test;user id=sa;password="  providerName="System.Data.SqlClient" />

</connectionStrings>

<system.web>

<caching>

<sqlCacheDependency enabled="true" pollTime="6000">

 <databases>

  <add name="Test" connectionStringName=" connectionstring " />

</databases>

</sqlCacheDependency>         

</caching> 

</system.web>

</configuration>

 

这里的connectionStringName指定了在<connectionStrings>中添加的某一个连接字符串。name则是为该SqlCacheDependency起的名字,这个名字将在第3步中用到。
SqlCacheDependency类会自动完成对此配置节信息的读取以建立和数据库之间的联系。

 

注意:

在<databases>节的<add name=" Test" connectionStringName=" connectionstring" />中的name属性值必须和第三步的Page_Load代码中System.Web.Caching.SqlCacheDependency("Test", "依赖的表名称(这里使用Contact)"); 中的第一个参数(数据库名称)相一致。

第二步:执行下述命令,为 数据库启用缓存依赖。

如果要配置SqlCacheDependency,则需要以命令行的方式执行。

aspnet_regsql.exe工具位于Windows\\Microsoft.NET\\Framework\\[版本]文件夹中。

aspnet_regsql -C "data source=127.0.0.1;initial catalog= Test;user id=sa;password=master" -ed -et -t " Contact "

参数-C后面的字符串是连接字符串(请替换成自己所需要的值),

参数-t后面的字符串是数据表的名字。

命令执行后,在指定的数据库中会多出一个AspNet_SqlCacheTablesForChangeNotification表。

第三步:在代码中使用缓存,并为其设置SqlCacheDependency依赖:

public static void SetCache(string CacheKey, object objObject, System.Web.Caching.CacheDependency dep)

{

    System.Web.Caching.Cache objCache = HttpRuntime.Cache;

    objCache.Insert(

        CacheKey,

        objObject,

        dep,

        System.Web.Caching.Cache.NoAbsoluteExpiration,//从不过期

        System.Web.Caching.Cache.NoSlidingExpiration,//禁用可调过期

        System.Web.Caching.CacheItemPriority.Default,

        null);

}

protected void Page_Load(object sender, EventArgs e)

{

    string CacheKey = "cachetest";

    object objModel = GetCache(CacheKey);//从缓存中获取

    if (objModel == null)//缓存里没有

    {

        objModel = GetData();//把当前时间进行缓存

        if (objModel != null)

        {

            //依赖数据库Test中的Contact表变化 来更新缓存

            System.Web.Caching.SqlCacheDependency dep = newSystem.Web.Caching.SqlCacheDependency("Test", " Contact ");

            SetCache(CacheKey, objModel, dep);//写入缓存

        }

    }

               

    rpdata.DataSource = (DataSet)objModel;

    rpdata.DataBind();

}

 

  

8、IIS网站发布-自定义domain

将网站部署到IIS的方式演示

修改C:\Windows\System32\Drivers\etc下的hosts文件

将127.0.0.1  www.stud.com

修改IIS站点的绑定

 

9、简单示例读写xml文件

①可以用XmlSerializer序列化
②XMLDocument
写:
  XmlDocument xml= new XmlDocument();
            XmlDeclaration xdec=  xml.CreateXmlDeclaration("1.0", "utf-8", "yes");
            xml.AppendChild(xdec);
            XmlElement rootElement = xml.CreateElement("root");
            xml.AppendChild(rootElement);
            XmlElement subElement = xml.CreateElement("subEle");
            subElement.SetAttribute("name", "kk");
            rootElement.AppendChild(subElement);
            xml.Save(@"c:\a.xml");
读:   XmlDocument doc = new XmlDocument();
            doc.Load("");
            XmlElement ele= doc.DocumentElement;//获取根节点
           XmlNodeList list=  ele.ChildNodes;//获取所有子节点
   foreach (XmlNode item in list)
            {
                if (item.NodeType==XmlNodeType.Element)//判断是元素节点才操作
                {

                }
            }
搜索指定节点,进行修改操作
doc.GetElementsByTagName("name");//获取指定名字的全部节点
③XDocument
写:
    XDocument xml = new XDocument();
            XDeclaration dec = new XDeclaration("1.0","utf-8",”no”);
            xml.Add(dec);
            XElement ele = new XElement("root");
            xml.Add(ele);
            ele.SetElementValue("", "");//也可以这样创建
            xml.Save("c:\\x.xml");
读:
  XDocument doc = XDocument.Load("");//加载指定的xml
            XElement root= doc.Root;//获取根节点
            root.Elements("name");//获取根节点下的name名字的节点
搜索指定节点,进行修改操作:
  XDocument doc = XDocument.Load("");//加载指定的xml
            XElement root = doc.Root;//获取根节点
            root.Descendants().Where (c=>c.Attribute("").ToString()=="");   // 按文档顺序返回此文档或元素的子代节点集合。
④Xpath  路径表达式
/      --从根目录开始
//aaa   --找到整篇文档的aaa所有节点
//*     --找到所有任何元素
//aaa/bbb --找到整篇文档的aaa所有节点中的bbb节点,bbb节点属于aaa的才能找到
/aaa/bbb/*  *代表所属节点的所有
/*/*/*/bbb
/aaa/bbb[0]   aaa节点下的第一个bbb
/aaa/bbb[last()]   aaa节点下的最后一个bbb
//@id   --@代表属性  ,选择所有元素中所具有id属性的属性,不是查找元素
//BBB[@id]  --在有id属性的BBB元素
用C#获取:XMLDocument document=new XMLDocument();
Document.load(“aa.xml”);
Document.SelectNodes(“/Users/user[id=3]”);//就是用这种语法,更新也是setattribute

10、有潜在的危险Request.Form值

在页面的指令集中使用VaildataRequest=false就行,不过有些低版本不行,还得在配置文件中配置<httpRuntime requestValidationMode="2.0"/>

11、配置跳转到同一的错误页面,MVC中的配置一样

<system.web>

    <customErrors mode="On" defaultRedirect="Error"></customErrors>

  </system.web>

部署网站的时候mode一定为false或者remote。defaultRedirect是跳转的页面

12、<configSections>    //一定紧贴configuration这个节点,否则会报错

13、重要:优化:在指定的bin目录下的指定程序集中查找,这样就不用在每个程序集中一一查找:

下面表示直接在CRM.Test 的dll中查找。

   <object id="myAnimal" type="TestSpring.Dog,CRM.Test" singleton="false">

猜你喜欢

转载自www.cnblogs.com/entclark/p/9098101.html
今日推荐