c#和.net 初见学习笔记(1)
c#和.net
背景介绍之类的就不再重复了,本次随笔记录从零开始学习c#和.net,有过java和python基础
版本
.net 6、visual studio 2022
接口和路由
个人习惯,学习时先看项目代码,看不明白的地方去查阅资料。从接口处切入
app.MapControllerRoute(
name: "default",
pattern: "{controller=Home}/{action=Index}/{id?}");
此处定义路由的基本格式
请求为 /controller/action/id 格式时自动路由到对应方法
controller 为控制器 (MVC)
cs文件创建在文件夹Controllers文件下
ps: 文件为DemoController.cs时 在路由格式中填写Demo即可
然后简易定义DemoController.cs 和其中方法
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using dotnetDemo.Controllers;
using Microsoft.AspNetCore.Mvc;
// For more information on enabling MVC for empty projects, visit https://go.microsoft.com/fwlink/?LinkID=397860
namespace dotnetDemo.Controllers
{
public class DemoController : Controller
{
// GET: /<controller>/
public string Detail(int id)
{
return "happy new year!";
}
}
}
此处定义了简单的 DemoController.cs和一个简单方法Detail
语法与java类似,不再赘述
请求 为/demo/detail
id为参数