MVC5学习系列——HelloWorld

来源:互联网 发布:网络性爱小说 编辑:程序博客网 时间:2024/06/08 08:27

MVC5学习笔记,其实就是敲了一遍官网代码。官网地址:http://www.asp.net/mvc

按照官网的教程,一步一步来:




按照以上几步,一个空的MVC程序就创建好了,这里命名DDZ.MVC5Test。下面我们调试一下:


看看这是什么原因,找了一下,发现在这里,程序起始页——路由配置:

路径:ddz.mvc5test\ddz.mvc5test\app_start\routeconfig.cs

</pre><pre name="code" class="csharp">public class RouteConfig    {        public static void RegisterRoutes(RouteCollection routes)        {            routes.IgnoreRoute("{resource}.axd/{*pathInfo}");            routes.MapRoute(                name: "Default",                url: "{controller}/{action}/{id}",                defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional }            );        }    }
这里应该是程序的入口,默认入口是 “Home”控制器中的“Index”方法。那现在我们就创建一个控制器,HomeController






创建好之后,在调试一下:



哎,有报错了,提示很明显,没有找到对应的文件。那我们按照官网,先这样处理:
 public class HomeController : Controller    {        //// GET: Home        //public ActionResult Index()        //{        //    return View();        //}        public string Index()        {            return "这是我的第一个MVC5应用程序!";        }    }
接着调试,如下图:


终于成功了!
访问地址可以是http://localhost:63743或者http://localhost:63743/home/index
下面我们按照路由规则,给方法添加参数:
 public string Index(string id)        {            return "参数ID为:" + id;        }
访问地址为:http://localhost:63743/home/index/6   或者 http://localhost:63743/home/index?id=9
我们将上面的方法,稍微变一下:
public string Index(string p1)        {            return "参数p1为:" + p1;        }
http://localhost:63743/home/index/9   直接这样访问是不行的,如下图:



在地址栏中写清楚:http://localhost:63743/home/index?p1=0   这样是可以的。或许http://localhost:63743/home/index/9 中的9因该是id的值,我们在修改一下上面的方法:
public string Index(string p1,string id)        {            return "参数p1为:" + p1 + "<br/>id为:" + id;        }
可以这样访问:http://localhost:63743/home/index/6?p1=9
或者http://localhost:63743/home/index?p1=9&id=6
或者只有一个参数http://localhost:63743/home/index?p1=9   (因为方法中参数都是string类型,所以可以少一个或者都不写。但是如果id是int类型就会报错了)
如果传值不小心落掉了一个参数,又不想它报错,我们可以这样,给int类型的参数设置默认值:
 public string Index(string p1, int id=0)        {            return "参数p1为:" + p1 + "<br/>id为:" + id;        }
下面我们再添加一个方法:
public string Welcome()        {            return "嗨,我来自Welcome方法!";        }
访问地址:http://localhost:63743/home/welcome
在Welcome中添加参数,方式和Index一样,这里就不说了。
这篇就写到这里了,谢谢!


2015-12-26
今天又发了一点新东西,在此篇基础上加点花絮。
        public string Index(string p)        {            return "参数ID为:" + p;        }
上面说http://localhost:63743/home/index/9  这样的地址访问 参数 p的值并不是9而是空。也就说在路由确定的前提下,地址栏中的参数9只能有id来接收。下面来验证一下,看一下下面的代码:
public string Index(string p)        {            RouteValueDictionary rvd = RouteData.Values;            return "参数ID为:" + p;        }
System.Web.Routing.RouteValueDictionary表示不区分大小写的键值对的集合,您可以在路由框架中的不同位置(例如,再定义路由的默认值时或者在生成基于路由的URL时)使用该集合。
访问地址是:http://localhost:56544/home/index/4下面看一下截图:


但是访问地址是:http://localhost:56544/home/index/4?p=qq     RouteValueDictionary 集合中却不包括参数p ,但是这时候可以用Request.Params["p"]获取地址栏中的参数了。我也晕了,看一下截图:


不知道有没有一个集合包含了所有的参数,但是现在我还没有发现。以后发现再补充,也希望大神指点。
下面我们再看看如何传递对象,先创建一个Student对象,再修改一下Index方法:
public class Student    {       public string StuName { get; set; }       public string StuAge { get; set; }    }
public string Index(Student stu)        {            RouteValueDictionary rvd = RouteData.Values;            if (stu.StuAge == null&& stu.StuName == null)            {                return "你没有输入!";            }            return "姓名:" + stu.StuName + "<br/>" + "年龄:" + stu.StuAge;        }
在地址栏中输入:http://localhost:56544/home/index?StuName=qq&StuAge=11   然后我们看一下截图:


亦或者我们为此单独创建一个——路由。
routes.MapRoute(               name: "HomeRoute",               url: "Home/Index/{StuName}/{StuAge}",               defaults: new { controller = "Home", action = "Index", StuName = UrlParameter.Optional, StuAge = UrlParameter.Optional }           );
之后我们可以这样访问了,地址如下:http://localhost:56544/home/index/qq/11   同样可以得到 Student对象。这次就补充到这里了。谢谢!










0 0
原创粉丝点击