16
2014
06

开发.net mvc3遇到奇怪的事情+解决方案

今天开发.net mvc3遇到奇怪的事情。

用火狐浏览器,打开index页面,请求index控制器很多次,查询数据库的代码多次执行,服务器变慢,
而且没刷新页面,过一段时间就有一两个请求,无限循环,这事从来没遇见过,真是一头雾水。

认真查看了一下原因,原来是图片的src,数据库中路径没有的情况下,src="/"  ,直接请求了默认的index控制器。

之后通过 在Index控制器中加一段处理代码处理了,而且还可以有图片没有的情况下,显示一张默认图片。

代码如下,加在控制器最前。

            if (Request.UrlReferrer != null)//某页面上图片请求的 UrlReferrer就是页面本身
            {
                string Accept = Request.Headers["Accept"];
                if (Accept.StartsWith("image/")  && Accept.Contains("application/")==false ) //图片请求一般是:image/png,image/*;q=0.8,*/*;q=0.5,IE和firefox中请求不同,页面请求也包含image/
                {
                    return File("/Content/images/Show/Show_pic07.jpg","image/jpeg");
                    //如果是图片src请求,返回一张默认图片,后面查询数据库的代码才不会重复执行。
            //如果不返回正确的图片,火狐浏览器会无限循环请求下去.
                }
            }

总结:本来10分钟能解决的事,花了半个小时 ,这和经验有关,开发过程中,得不断积累经验。

08
2014
06

qtday03 简单版的贪吃蛇游戏

//snakegame.h

#ifndef SNAKEGAME_H
#define SNAKEGAME_H
#include<QList>
#include<QtWidgets/QLabel>
#include<QtWidgets/QDialog>
#include<QKeyEvent>
#include<QTimer>
/*枚举,表示方向*/
enum Direction{D_UP,D_DOWN,D_LEFT,D_RIGHT};
class SnakeGame:public QDialog
{
    Q_OBJECT
public:
    SnakeGame() ;
    ~SnakeGame();
public slots:
    void snakemove();/*移动*/
public:
    QLabel* getfood();/*产生新食物*/
    void keyPressEvent(QKeyEvent *key);
private:
    QList<QLabel*> snake;/*蛇*/
    QLabel * food;/*食物*/
    Direction dire;/*方向*/
    int foodlen;/*食物长度*/
    int maxlen;/*最大长度*/
    QTimer * timer;/*定时器*/
};
#endif // SNAKEGAME_H


08
2014
06

qtday02 qt做简单的加法器和模拟登陆功能

//在3t2目录下

//adder.h

/*
qt简单加法计算器
*/
#ifndef ADDER_H
#define ADDER_H
#include<QDialog>
#include<QLineEdit>
#include<QtWidgets/QPushButton>
#include<QtWidgets/QLabel>
class Adder:public QDialog{
    Q_OBJECT//让自定义的槽函数生效
    private:
        QLineEdit * add1;
        QLabel * oper;
        QLineEdit *add2;
        QPushButton * equ;
        QLineEdit * res;
        QPushButton * bclose;
    public:
        Adder();
        ~Adder();
        /*自定义槽函数*/
    public slots:
        void getres();
    
};
#endif


08
2014
06

qtday01 ubuntu 下手动编写第一个qt程序

//qt 5.2.1

//1t目录下

//1t.cpp

/*
 第一个qt程序
 */
#include <QApplication>
#include<QtWidgets/QLabel>
#include<QString>
#include<QTextCodec>
int main(int argc,char** argv){
    /*构造一个对象*/
    QApplication app(argc,argv);
    QTextCodec* coder=QTextCodec::codecForName("utf-8");
    /*设置编码对象*/
//    QTextCodec::setCodecForTr(coder);
    //set code to the words
    //QLabel qlab(QObject::tr("hello qt你好 ,today is a nice day!"));
    QLabel qlab(coder->toUnicode("hello qt你好 ,today is a nice day!"));
    qlab.show();
    /*让程序进入事件循环*/
    return app.exec();
}

//在1t目录下,输入命令:

«1»