ASP.NET MVC学习笔记

参考ASP.NET MVC 教程,这教程非常详细。
只记录过程中我觉得比较模糊的部分。

Log日志

绝对路径

string strFilePath = @"C:\Users\80029323\Desktop\My Files\LogInfo.txt";

相对路径(获取项目下的文件夹):

 string folder = System.Web.HttpContext.Current.Server.MapPath("~/log");

判断LogInfo文件是否存在,不存在就新建一个

using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Web;

namespace e_form_mvc.App_Code
{
    public class Log
    {

        public static void WriteLog(string _msg)
        {
            string folder = System.Web.HttpContext.Current.Server.MapPath("~/log");
            if (!Directory.Exists(folder)) Directory.CreateDirectory(folder);

            string filename = folder + "\\Log.txt";
            if (File.Exists(filename))
            {
                FileStream fs = new FileStream(filename, FileMode.Append, FileAccess.Write, FileShare.ReadWrite);
                StreamWriter sr = new StreamWriter(fs);
                sr.WriteLine(DateTime.Now.ToString("yyyy/MM/dd HH:mm:ss") + "\t" + _msg);
                sr.Close();
                fs.Close();
            }
            else
            {
                FileStream fs = new FileStream(filename, FileMode.Create, FileAccess.Write, FileShare.ReadWrite);
                StreamWriter sr = new StreamWriter(fs);
                sr.WriteLine(DateTime.Now.ToString("yyyy/MM/dd HH:mm:ss") + "\n" + _msg);
                sr.Close();
                fs.Close();
            }
        }
    }
}
   Log.WriteLog("网站启动");

在这里插入图片描述

controller向view传值

ViewData传递集合到view

首先准备Model

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;

namespace e_form_mvc.Models
{
    public class production
    {
        public string fileID { get; set; }

        public string customer { get; set; }

        public string description { get; set; }
        public string paperID { get; set; }
        public string change { get; set; }

        public string checkDate { get; set; }

        public int principal { get; set; }
    }
}

controller里

ViewData["Data"] = new List<production>()
            {
                new production {
                    fileID="DR-200001",
                    customer="test",
                    description="great",
                    paperID="test1",
                    change="a lot",
                    checkDate=DateTime.Now.ToString("yyyy/MM/dd HH:mm:ss"),
                    principal=80029323,
                },
               new production {
                    fileID="DR-200001",
                    customer="test",
                    description="great",
                    paperID="test2",
                    change="a lot",
                    checkDate=DateTime.Now.ToString("yyyy/MM/dd HH:mm:ss"),
                    principal=80029323,
                },
            };

view

@using e_form_mvc.Models //必须引用Models
@{
    ViewBag.Title = "Index";
    //类型转换 viewdata是对象
    var list = ViewData["Data"] as List<production>;
}

<h2>Index</h2>
<div>
    @foreach(var item in list)
    {
        <div>
            <label>文件编号</label>@item.fileID
        </div>
        <div>
            <label>图纸编号</label>@item.paperID
        </div>
        <div>
            <label>客户名称</label>@item.customer
        </div>
    }
</div>

在这里插入图片描述

Model向view传值

controller

        public ActionResult model()
        {
            production product = new production {
                 fileID="DR-200001",
                 customer="test"


            };
            return View(product);
        }

view

<div>
    fileid:@Model.fileID
</div>
<div>
    customer:@Model.customer
</div>

在这里插入图片描述

viewData和viewBag区别
ViewDataViewBag
key/value字典集合dynamic类型对象
ASP.NET MVC1引入ASP.NET MVC3引入
基于ASP.NET 3.5 framework基于ASP.NET 4.0 framework
比ViewBag快比ViewData慢
需要转换类型不需要转换
有一些类型转换代码可读性更好

两个Web.config文件

  • 根目录下的Web.config配置文件主要是用来配置数据库连接字符串、日志输出路径等信息的,比如配置数据库连接字符串;
  • Views文件夹下面的配置文件主要是用来引入一些cshtml页面中的命名空间,在namespaces下引入之后相当于为每个页面都引用了此模型。
    在这里插入图片描述

路由规则

原则:如果有多条路由都和这个URL匹配,那么就是排在最前面的优先匹配,排在后面的就不在进行匹配。

从控制器获取url值的三种方式

举例url: http://localhost:54698/Home/Index/2?name=klaus

            string name = Request.QueryString["name"];//klaus 获取参数
            if (RouteData.Values["id"] != null) {
                paraid = RouteData.Values["id"].ToString();//2 获取id
            }

第三种action参数我还不是很理解。
在这里插入图片描述

  • Request.QueryString只能获取以?分隔的参数值!
  • RouteDate.Values[“id”]就是当前字典上的路由数据,通过访问键名的方式得到键值,比如URL模式匹配的字符串是id,这里必须是id。
  • Action方法的参数和路由字典的参数是对应的,MVC框架在执行action之前会首先为这些参数赋值。

由路由到url

在这里插入图片描述
只需两步

 VirtualPathData vp = RouteTable.Routes.GetVirtualPath(null, new RouteValueDictionary(new { controller = "Home", action = "Index", id = 4 }));
 string url = vp.VirtualPath;

_ViewStart.cshtml的作用

页面启动顺序:

  • _ViewStart.cshtml
  • Index.cshtml
  • Layout.cshtml

_ViewStart.cshtml是一个在呈现View文件的时候的启动文件,会在所有View(.cshtml)被执行之前执行,主要用于一些不方便或不能再母版(_Layout.cshtml)中进行的统一操作。譬如你有很多个没有继承关系的母版或不使用母版的单页。
如果不想使用母版页,那么就将layout设置为null,或者在创建视图时不勾选母版。

@{
    ViewBag.Title = "TestView";
    // 指定Layout为null则表示不在使用布局页
    Layout = null;
}
  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值