zoukankan      html  css  js  c++  java
  • 【2013年】开发常见问题回顾(一)

    【2013年】开发常见问题回顾(一)

    记录开发中遇到的和别人问的较多的问题....

    目录

    IE10中LinkButton不可用

        这应该是2013年初遇到的一个BUG,当使用Asp.Net开发Web Application时,页面使用LinkButton按钮;运行页面用IE10打开,点击LinkButton按钮出现如下图错误

      没有直接弹出如下图错误,可以运行IE10开发人员工具(F12),在控制台中查看,也会输出 “__doPostBack”未定义 错误信息

    解决方法及参考:

    下载并在服务器上安装相应补丁程序即可,下载地址:

    .NET Framework 2.0 : http://support.microsoft.com/hotfix/KBHotfix.aspx?kbnum=2600100&kbln=zh-cn

    .NET Framework 4.0 : http://support.microsoft.com/hotfix/KBHotfix.aspx?kbnum=2600088&kbln=zh-cn

    参考来自:

    http://www.hanselman.com/blog/BugAndFixASPNETFailsToDetectIE10CausingDoPostBackIsUndefinedJavaScriptErrorOrMaintainFF5ScrollbarPosition.aspx

    如何配置IIS才能通过链接下载文件

     如果不进行任何的配置,通过url直接下载一个doc类型文件(例如:http://www.xxx.com/1.doc),页面会返回如下错误:

    HTTP 错误 404.3 - Not Found

    由于扩展配置问题而无法提供您请求的页面。如果该页面是脚本,请添加处理程序。如果应下载文件,请添加 MIME 映射。

     解决方法:

     以IIS7.5为例

     1.在IIS中找到相应的网站

     2.在“功能视图”中找到“MIME”类型,双击进入

     3.添加,在“文件扩展名”内填入相应的扩展名,比如:.doc

     4.在“MIME类型(M)”内填入相应的MIME类型,比如doc文件的MIME:application/msword

     5.提交

     操作完成后再次点击url,浏览器会填出下载窗口!

    不清楚相应类型文件对应的MIME类型可以在此网址查找:

     http://www.filesuffix.com/

    测试发现应该不用每种文件类型指定确切的MIME类型,application/octet-stream 适用于多数文件类型...

    如何配置IIS通过链接是下载而不是直接打开txt/图片类型文件

    当正确配置MIME类型后,下载文件大部分都可以成功,但是如txt或者是一些图片格式的文件,浏览器不会填出下载窗口,而是会在当前页打开并显示其内容,如果需要直接下载这些类型的文件,还需要其它的IIS配置:

      测试只适用于IE!

     解决方法:

     以IIS7.5为例

     1.在IIS中找到相应的网站

     2.在“功能视图”中找到“HTTP 响应头”类型,双击进入

     3.添加,“名称”内填入:Content-Disposition

     4.在“值”内填入:attachment

     5.提交

     操作完成后,打开IE再次点击url,浏览器会填出下载窗口!

    C# 给虚拟目录批量添加MIME示例

     展开查看示例代码

    using System.DirectoryServices;
    //添加导出COM组件:Active DS IIS Namespace Provider

    static void Main(string[] args)
    {

    Console.WriteLine("正在添加已有的MIME类型...");

    DirectoryEntry virDir_IKeyMgmt = new DirectoryEntry("IIS://localhost/W3SVC/1/ROOT/虚拟目录名");

    try
    {
    PropertyValueCollection mimeCollection = (PropertyValueCollection)virDir_IKeyMgmt.Properties["MimeMap"]; //获取MIME类型

    string[] extArr = new string[] {".tar",
    ".zip",
    ".gz",
    ".gds",
    ".v",
    ".sof",
    ".bit",
    ".stp",
    ".edf",
    ".sdc",
    ".ucf",
    ".fsdb",
    ".vcd",
    ".sess",
    ".rc",
    ".result",
    ".jpg",
    ".png",
    ".pdf",
    ".doc",
    ".txt",
    ".log",
    ".xrc",
    ".lvs",
    ".xls",
    ".elf",
    ".tr0"};

    IISOle.MimeMapClass mimeType = null;

    foreach (string ext in extArr)
    {
    mimeType = new IISOle.MimeMapClass();
    mimeType.Extension = ext;
    mimeType.MimeType = "application/octet-stream";
    mimeCollection.Add(mimeType);
    }

    virDir_IKeyMgmt.CommitChanges();//更改目录

    Console.WriteLine("添加成功!");
    }
    catch (Exception ex)
    {

    Console.WriteLine("添加失败!");
    Console.WriteLine("错误信息:" + ex.ToString());
    }
    finally
    {
    Console.ReadLine();
    }
    }

    展开查看示例代码

    C# 获取虚拟目录的物理路径示例

     展开查看示例代码

    /// <summary>
    /// 获取虚拟目录的物理路径
    /// </summary>
    /// <param name="identifier">虚拟目录所属网站的标识符</param>
    /// <param name="name">虚拟目录名称</param>
    /// <returns></returns>
    private string GetWebVirtualDirectoryPath(string identifier, string name)
    {
    DirectoryEntry de = new DirectoryEntry("IIS://LOCALHOST/W3SVC/" + identifier + "/ROOT/" + name);
    string path = (string)de.Properties["Path"].Value;

    return path;
    }

    /// <summary>????????
    /// 获取网站的标识符
    /// </summary>
    /// <param name="portNumber">端口号</param>
    ///<returns></returns>
    private string GetWebSiteIdentifier(string portNumber)
    {
    DirectoryEntry root = new DirectoryEntry("IIS://LOCALHOST/W3SVC");
    foreach (DirectoryEntry e in root.Children)
    {
    if (e.SchemaClassName == "IIsWebServer")
    {
    foreach (object property in e.Properties["ServerBindings"])
    {
    if (property.Equals(":" + portNumber + ":"))
    {
    return e.Name;
    }
    }
    }
    }

    //?默认为“默认网站”的标识符
    return "1";
    }

    展开查看示例代码

    The name 'ScriptManager' does not exist in the current context

     错误如下图:

     解决方式:

     1.从.aspx页移除ScriptManager控件

     2.页面的.cs文件添加using System.Web.UI;

     3.再把ScriptManager控件添加到.aspx页

     4.重新生成

    System.InvalidOperationException: 未在本地计算机上注册“Microsoft.ACE.OLEDB.12.0”提供程序 Office 2010

     可能出现此问题的情况有很多

     1.连接字符串没有配置正确,可对照相应的版本 http://www.connectionstrings.com/excel/

     2.需要安装Microsoft Access 2010 数据库引擎可再发行程序包 http://www.microsoft.com/en-us/download/confirmation.aspx?id=23734

     3.如果以上两种情况都解决不了,可以试着修改项目的项目PlatForm为x86

    Unable to find manifest signing certificate in the certificate store"

     解决方法:

     以VS2008为例

     1.VS Solution Explorer中选择项目

     2.右键,点击进入“properties”,选中“Signing”标签页

     3.找到“Sign the ClickOnce manifests”选择,去掉勾选并保存。

    配置Session为StateServer的方式

      配置方法:

     1.修改注册表项

     找到 HKEY_LOCAL_MACHINE/SYSTEM/CurrentControlSet/Services/aspnet_state/Parameters

     把AllowRemoteConnection设为1

     2.启动ASP.NET State Service服务

     把ASP.NET State Service服务设为自动,并且启动它

     如果对Session状态的配置方法有疑问可以看看MSDN :

     http://msdn.microsoft.com/zh-cn/library/h6bb9cz9(v=VS.85).aspx

     如果对Session有哪几种保存方式、分别有什么优劣这些都不是很了解,自已去看看MSDN 看看这篇文章,相信会对Session有更进一步的理解:

     http://blog.csdn.net/cityhunter172/article/details/727743

    C# 用WMI获取网卡MAC地址示例代码

     展开查看示例代码

    static void Main(string[] args)
    {
    string filePath = @"文件绝对路径";

    System.IO.FileStream fs = new System.IO.FileStream(filePath, System.IO.FileMode.Open, System.IO.FileAccess.Read);
    System.IO.BinaryReader r = new System.IO.BinaryReader(fs);
    string bx = "";
    byte buffer;
    try
    {
    buffer = r.ReadByte();
    bx = buffer.ToString();
    buffer = r.ReadByte();
    bx += buffer.ToString();
    }
    catch (Exception exc)
    {
    Console.WriteLine(exc.Message);
    }
    r.Close();
    fs.Close();

    Console.WriteLine(bx);
    Console.ReadLine();
    }

    展开查看示例代码

    使用Net User命名创建帐户,如何让密码永不过期的问题

     程序通过调用net user命令创建用户,但是此命令未提供设置密码永不过期的参数,如果想设置密码永不过期,可以通过调用第三方Netuser.exe来完成。

     下载地址: http://files.cnblogs.com/zhongweiv/NetUser.zip

     将 netuser.exe 拷贝到 %systemroot%system32 下。

     Examples:

     <Setting >:

      /name: set a new name

      /pwnexp:{y|n} set 'password never expires'

     1. 更改用户名

     netuser Administrator /name:"Admin go"

     更改Administrator名字为 Admin go

     netuser "John Doe" /name:DoeJ

     更改John Doe名字为DoeJ

     2.是用户用不过期

     netuser admin /pwnexp:y

     使admin 用户永不过期

     net user这些操作可以通过 Network Management Functions 的User相关的方法去实现!

    C# 得到文件头信息示例代码

     展开查看示例代码

    static void Main(string[] args)
    {
    string filePath = @"文件绝对路径";

    System.IO.FileStream fs = new System.IO.FileStream(filePath, System.IO.FileMode.Open, System.IO.FileAccess.Read);
    System.IO.BinaryReader r = new System.IO.BinaryReader(fs);
    string bx = "";
    byte buffer;
    try
    {
    buffer = r.ReadByte();
    bx = buffer.ToString();
    buffer = r.ReadByte();
    bx += buffer.ToString();
    }
    catch (Exception exc)
    {
    Console.WriteLine(exc.Message);
    }
    r.Close();
    fs.Close();

    Console.WriteLine(bx);
    Console.ReadLine();
    }

    展开查看示例代码

     文件头并不是确定文件类型的准确标准,但确实能判断出一些文件,本示例其实也不能叫得到文件头的信息,只是读取了文件的前两个字节,如果作为判断文件的严谨依据,还是要根据具体文件去进去格式分析!

    在应用程序级别之外使用注册为 allowDefinition='MachineToApplication' 的节是错误的。如果在 IIS 中没有将虚拟目录配置为应用程序

     以IIS6为例

     解决方式:

     1.在IIS中网站对应的虚拟目录上右键,选属性

     2.应用程序名后点创建

    C# 利用SharpZipLib对字符串进行压缩

     下载地址

     https://github.com/icsharpcode/SharpZipLib

     展开查看示例代码

    #region## 压缩字符串
    /// <summary>
    /// 功能:压缩字符串
    /// 创建人:Wilson
    /// 创建时间:2012-12-27
    /// </summary>
    /// <param name="str"></param>
    /// <returns></returns>
    public static string Compress(string str)
    {
    byte[] temp = System.Text.Encoding.Unicode.GetBytes(str);
    System.IO.MemoryStream memStream = new System.IO.MemoryStream();
    System.IO.Stream stream = new ICSharpCode.SharpZipLib.BZip2.BZip2OutputStream(memStream);
    stream.Write(temp, 0, temp.Length);
    stream.Close();
    byte[] compressedData = (byte[])memStream.ToArray();
    return System.Convert.ToBase64String(compressedData, 0, compressedData.Length);
    }
    #endregion

    #region## 解压缩字符串
    /// <summary>
    /// 功能:解压缩字符串
    /// 创建人:Wilson
    /// 创建时间:2012-12-27
    /// </summary>
    /// <param name="compressedStr"></param>
    /// <returns></returns>
    public static string UnCompress(string compressedStr)
    {
    System.Text.StringBuilder sb = new System.Text.StringBuilder();
    int totalLen = 0;
    byte[] temp = System.Convert.FromBase64String(compressedStr); ;
    byte[] writeTemp = new byte[4096];
    System.IO.Stream stream = new ICSharpCode.SharpZipLib.BZip2.BZip2InputStream(new System.IO.MemoryStream(temp));
    while (true)
    {
    int size = stream.Read(writeTemp, 0, writeTemp.Length);
    if (size > 0)
    {
    totalLen += size;
    sb.Append(System.Text.Encoding.Unicode.GetString(writeTemp, 0, size));
    }
    else
    {
    break;
    }
    }
    stream.Close();
    return sb.ToString();
    }
    #endregion

    展开查看示例代码

    Assembly.Load (Byte[])方法 调用内存占用一直增大的问题

    问题表现:动态调用WebSerivce时,因为反复调用使用了Assembly.Load (Byte[]),导致进程内存不断升高

    解决方法:

    复制代码
    //方法外声明
    private static byte[] filedata = null;
    private static Assembly asm = null;
    
    //方法中使用
    if (filedata == null)
    {
        filedata = File.ReadAllBytes(DLL_NAME);                    
    }
    
    if (asm == null)
     {
        asm = Assembly.Load(filedata);
    }
    复制代码

    IIS7/7.5配置上传大文件

     在IIS7/7.5中要上传在文件,不仅需要配置

    <httpRuntime executionTimeout="3600" maxRequestLength="2097151"/> 

     还需要配置如下requestLimits节点

    复制代码
    <system.webServer>
       <security>
          <requestFiltering>
            <requestLimits maxAllowedContentLength="2147483647"/>     
          </requestFiltering>
        </security>    
     </system.webServer>
    复制代码

     PS:

     executionTimeout单位:秒

     maxRequestLength单位:KB

     maxAllowedContentLength:bytes

     maxRequestLength 表示Asp.Net允许上传的大小  默认值:4096KB 最大值:2097151KB(2G-1K)

     executionTimeout 表示允许执行的最大时间 默认值是90秒 (超时只有在compilation 节点设置为时才会生效)

     httpRuntime 元素(ASP.NET 设置架构)

     http://msdn.microsoft.com/zh-cn/library/e1f13641(v=vs.90).aspx

     system.webServer节点是IIS7引入的

     不要轻易修改上传限制,以防上传大文件攻击服务器!

    项目发布在IIS中图片或CSS样式无法正常显示

     很多时候在开发环境中页面能正常显示,但发布在IIS中后,显示正常

     最常见原因:

     1.路径不对正常

        特别是发布为虚拟目录时,一定要注意路径问题

     2.IIS安装没有勾选“静态内容“选项

        多数这种原因比较多,打开IIS配置窗口

        Internet Information Services(Internet 信息服务)-->World Wild Web(万维网服务)-->Common HTTP features(常见HTTP功能)-->选中staticcontent(静态内容)

     重新刷新页面即可

    Web.config文件中配置修改查询超时时间

     进行大数据查询或者统计数据时,常出现查询超时,通过配置Web.config连接字符串可以解决(MySQL)

    Server=211.136.8.81;Port=3306;Database=lf;Uid=root;Pwd=admin123;CharSet=utf8;Pooling=True;default command timeout=3600;Connection Timeout=3600;

    default command timeout和Connection Timeout从字面上就很好理解,就不解释了

    友情提示:3600这个值只是示例,具体还是要配置一个相对合理的时间,资源宝贵!

    作   者:   Porschev[钟慰] 
    出   处:   http://www.cnblogs.com/zhongweiv/ 
    微   博:     http://weibo.com/porschev 
    欢迎任何形式的转载,但请务必注明原文详细链接

     
    分类: C#常见错误ASP.NET
    标签: 常见错误
  • 相关阅读:
    PLL详解
    CSI-2 协议
    C语言volatile关键字的用法
    联合体(union)的使用方法及其本质
    Linux驱动之LED驱动编写
    insmod 和第一个驱动
    黑电平校正BLC
    什么是HDR?
    使用starUML画顺序图
    UML 资料整理
  • 原文地址:https://www.cnblogs.com/Leo_wl/p/3346386.html
Copyright © 2011-2022 走看看