MVC01
1.Controller
1) 添加:
在Controller目录右键进行添加,出现很多模式供选择,选择空的Controller,命名后新建。新建后Views
目录将同步生成相应名称的视图文件目录
均继承于Controller类
控制器内的方法默认返回ActionResultl类型,可自行修改
修改后可运行并在域名后加入自动生成的Views目录下的文件名称,就可以访问到该路由
该路由通过/Hello访问
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc; namespace HelloMVC.Controllers
{
public class HelloController : Controller
{
// GET: Hello
public string Index()
{
return "Hello MVC";
}
}
}
也可以新建自己的方法(路由):该路由通过/Hello/Yes访问
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc; namespace HelloMVC.Controllers
{
public class HelloController : Controller
{
// GET: Hello
public string Index()
{
return "Hello MVC";
} public string Yes()
{
return "Yse MVC, this is Yes.";
}
}
}
如果要进行url传参,就为上述方法添加参数
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc; namespace HelloMVC.Controllers
{
public class HelloController : Controller
{
// GET: Hello
public string Index()
{
return "Hello MVC";
} public string Yes(string name)
{
return "Yse MVC, this is Yes." + name; }
}
}
但这么做比较不安全
通常接收用户传参时我们先进行一个编码:
也可为传参添加缺省值
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc; namespace HelloMVC.Controllers
{
public class HelloController : Controller
{
// GET: Hello
public string Index()
{
return "Hello MVC";
} // 参数缺省值
public string Yes(string name = "Linda")
{
return "Yse MVC, this is Yes." + HttpUtility.HtmlEncode(name);
//或 return "Yse MVC, this is Yes." + Server.HtmlEncode(name); }
}
}
小技巧:F5键 Debug模式,执行断点
ctrl+F5 Debug模式但不执行断点
测试时将为我们使用IIS搭建一个建议的服务器
Global.asax文件可以查看路由的一些配置
RegisterRoutes方法
2)调试技巧:
MVC01的更多相关文章
- 快速入门系列--MVC--01概述
虽然使用MVC已经不少年,相关技术的学习进行了多次,但是很多技术思路的理解其实都不够深入.其实就在MVC框架中有很多设计模式和设计思路的体现,例如DependencyResolver类就包含我们常见的 ...
- MVC-01 概述
一.何谓MVC 1.MVC是开发时所使用的一种架构(框架). 2.目的在于简化软件开发的复杂度,以一种概念简单却又权责分明的架构,贯穿整个软件开发流程,通过“商业逻辑层”与“数据表现层”的切割,让这两 ...
- 快速入门系列--MVC--02路由
现在补上URL路由的学习,至于蒋老师自建的MVC小引擎和相关案例就放在论文提交后再实践咯.通过ASP.NET的路由系统,可以完成请求URL与物理文件的分离,其优点是:灵活性.可读性.SEO优化.接下来 ...
- 快速入门系列--MVC--07与HTML5移动开发的结合
现在移动互联网的盛行,跨平台并兼容不同设备的HTML5越来越盛行,很多公司都在将自己过去的非HTML5网站应用渐进式的转化为HTML5应用,使得一套代码可以兼容不同的物理终端设备和浏览器,极大的提高了 ...
- MVC EF 增 删 改 查
using System;using System.Collections.Generic;using System.Linq;using System.Web;//using System.Data ...
随机推荐
- BeagleboneBlack上u-boot的MLO文件是哪里来的
在玩BeagleboneBlack一段时间之后不可避免地接触到了u-boot,之前的玩耍过程大致上是这样的: 在MATLAB下耍,因为MATLAB提供了它的硬件支持,可以直接在命令行与之交互,也可在s ...
- Java 线程池(二)
简介 在上篇 Java 线程池(一) 我们介绍了线程池中一些的重要参数和具体含义,这篇我们看一看在 Java 中是如何去实现线程池的,要想用好线程池,只知其然是远远不够的,我们需要深入实现源码去了解线 ...
- TCP与UDP 笔记
本文整理自:<图解TCP/IP 第5版>作者:[日] 竹下隆史,[日] 村山公保,[日] 荒井透,[日] 苅田幸雄 著译者:乌尼日其其格出版时间:2013-07 TCP提供可靠的通信传输, ...
- [LC] 121. Best Time to Buy and Sell Stock
Say you have an array for which the ith element is the price of a given stock on day i. If you were ...
- iOS数据锁
简介 当一个线程访问数据时,而其他数据不能进行访问,保证线程安全或者可以理解为执行多线程,对于共享资源访问时保证互斥的要求 文章 不再安全的 OSSpinLock iOS开发中的11种锁以及性能对比 ...
- [LC] 91. Decode Ways
A message containing letters from A-Z is being encoded to numbers using the following mapping: 'A' - ...
- fare|gave it away|catch a glimpse |involve|rip|eternalstiff|
N-COUNT 旅费;路费;车费A fare is the money that you pay for a journey that you make, for example, in a bus, ...
- Android Studio调用系统隐藏接口EthernetManager
google source签名文件参考:https://android.googlesource.com/platform/build/+/donut-release/target/product/s ...
- win7+centos6.5安装双系统
前言:之前在琢磨怎么安装双系统 倒腾了两天终于给装上了 使用软件 镜像:CentOS-6.5-x86_64-bin-DVD1.iso 开机引导软件 easybcd2.2 u盘制作软件 USBWrite ...
- ionic3 生命周期钩子
ionViewDidLoad 页面加载完成触发,这里的"加载完成"指的是页面所需的资源已经加载完成,但还没进入这个页面的状态(用户看到的还是上一个页面). 需要注意的是它是一个很傲 ...