ASP.NET Core MVC入门——2
如何识别到Controller:
asp.net core mvc是通过URL来识别请求中的控制器,默认URL路由逻辑是
/[Controller]/[ActionName]/[Parameters]
默认URL的路由格式在项目中的Startup.cs中有定义,ActionName表示控制器类中的方法。
app.UseEndpoints(endpoints => { endpoints.MapControllerRoute( name: "default", pattern: "{controller=Home}/{action=Index}/{id?}"); });
如果URL 是 localhost:59191/HelloWorld,则表示对应访问HelloWorldController类的Index方法。
如果URL 是 localhost:59191/HelloWorld/Welcome,则表示对应访问HelloWorldController类的Welcome方法。
我们尝试修改Welcome方法如下
public string Welcome(string name, int numTimes = 1) { return HtmlEncoder.Default.Encode($"Hello {name}, NumTimes is: {numTimes}"); }
如果URL 是 localhost:59191/HelloWorld/Welcome?name=Money&numTimes=10,则会显示10次Money。
MVC中的View
1 在Views文件夹上右键,添加 > 新建文件夹,命名为HelloWorld。
2 右键HelloWorld文件夹,添加“视图”,取名为Index,则会生成一个Index.cshtml,这是一个Razor视图文件,Razor是一个类似html的可以用C#编写的文件类型,方便将数据模型和视图进行绑定使用。
3 Index.cshtml文件内容改为如下,
@{ ViewData["Title"] = "Index"; } <h2>Index</h2> <p>Hello from our View Template!</p>
4 将HelloWorldController中的index方法改为如下
public IActionResult Index() { return View(); }
5 此时,就可以再次运行,发现 localhost:59191/HelloWorld的页面是Index.cshtml中的效果。控制器中Index方法只有一条语句return View()。表示需要返回一个view视图类型来回复给用户。这里View()中没有指明使用哪个视图文件为空,asp.net的MVC会使用默认视图,即方法名称对应的视图,即HelloWorld目录下的index.cshtml文件。
Views/Shared/_Layout.cshtml
这个文件是所有视图文件都会使用的模板,可以通过修改这个文件修改所有视图公共的部分。
打开该文件,里面有一行如下
<title>@ViewData["Title"] - Movie App</title>
ViewData是一个字典类型的变量,可以在控制器和视图中使用,如上这行表示使用ViewData中名为Title的值,这个Title是每个页面自己定义的,这样就会根据不同页面显示不同的值,从这里也可以看出,每个视图先执行,然后执行共享的视图。
ViewData
控制器中的数据如何传递给视图显示呢?
我们这里看个示例:将HelloWorld中Welcome改为如下,有两个参数。通过ViewData成员变量传入Message和NumTimes
public IActionResult Welcome(string name, int numTimes = 1) { ViewData["Message"] = "Hello " + name; ViewData["NumTimes"] = numTimes; return View(); }
如果通过URL为 localhost:59191/HelloWorld/Welcome?name=Money&numTimes=10,则name和numTimes自动映射到参数上。这里没有大小写之分。
创建Welcome视图,其中代码如下,通过ViewData使用Message和NumTimes。@表示其后使用C#语法。
@{ ViewData["Title"] = "Welcome"; } <h2>Welcome</h2> <ul> @for (int i = 0; i < (int)ViewData["NumTimes"]; i++) { <li>@ViewData["Message"]</li> } </ul>
- 基于Asp.Net Core Mvc和EntityFramework Core 的实战入门教程系列-3
- ASP.NET Core 入门教程 3、ASP.NET Core MVC路由入门
- ASP.NET Core快速入门(第6章:ASP.NET Core MVC)--学习笔记
- ASP.NET Core MVC入门——3
- ASP.NET Core MVC 和 Visual Studio入门(五) 使用 SQL Server LocalDB
- ASP.NET Core MVC 与 Visual Studio 入门
- ASP.NET Core MVC 和 Visual Studio入门 (一)安装Visual Studio和.NET Core
- 基于Asp.Net Core Mvc和EntityFramework Core 的实战入门教程系列-1
- Visual Studio ASP.NET Core MVC入门教程第一篇
- ASP.NET Core 入门教程 6、ASP.NET Core MVC 视图布局入门
- ASP.NET Core 入门教程 5、ASP.NET Core MVC 视图传值入门
- 【ASP.NET Core快速入门】(五)命令行配置、Json文件配置、Bind读取配置到C#实例、在Core Mvc中使用Options
- ASP.NET Core 入门教程 4、ASP.NET Core MVC控制器入门
- ASP.NET Core MVC 和 Visual Studio入门(三) 添加视图
- 菜鸟入门【ASP.NET Core】5:命令行配置、Json文件配置、Bind读取配置到C#实例、在Core Mvc中使用Options
- ASP.NET Core MVC 和 Visual Studio入门(四)添加模型
- 使用 Visual Studio 的 ASP.NET Core MVC 和 Entity Framework Core 入门 (1 of 10)
- ASP.NET Core 入门教程 7、ASP.NET Core MVC 分部视图入门
- 【视频】ASP.NET Core MVC 2.* 入门
- 基于Asp.Net Core Mvc和EntityFramework Core 的实战入门教程系列-1