1.添加一个控制器类 命名为HelloWorld :
public class helloWorldController : Controller
{
// GET: helloWorld
public string Index() //http://localhost:10881/helloWorld/Index
{
return "helloWorld";
}
public string Hello() //http://localhost:10881/helloWorld/hello
{
return "hello erevyone";
}
public string Hello1(string name) //http://localhost:10881/helloWorld/hello1?name=jim
{
return "how to you"+ name ;
}
}
在浏览器中分别访问http://localhost:10881/helloWorld/hello ,http://localhost:10881/helloWorld/hello, http://localhost:10881/helloWorld/hello1?name=jim
URL会调用controller相应方法,会得到不同的问候 内容(10881是本机的端口号,不同机子会不一样)
2通常,我们并不直接在Controller中返回字符串,而是返回一个View,然后添加一个View 在view里面输出我们想要的字符串
添加一个controller 命名为Index 代码不做修改 ,右键添加一个视图
@{ ViewBag.Title = "Index"; } < h2 >Hello My First mvc View </ h2 >
F5 运行http://localhost:10881/View/Index 得到页面
在controller类添加一句ViewBag.Count=4(可以用来在controller和页面之间传递数据) 然后在视图中添加代码
<ul>
@for (int i = 0; i < ViewBag.Count; i++)
{
<li>Hello @i</li>
}
</ul>
F5 运行http://localhost:10881/View/Index 得到页面