asp.net core 6.0

asp.net core 6.0

文件认识

Connected Services:

启动服务

launchSettings.json:

发布设置

{
  "iisSettings": {
    "windowsAuthentication": false,//是否开启windows认证
    "anonymousAuthentication": true,//是否开启匿名访问
    "iisExpress": {
      "applicationUrl": "http://localhost:61539",//iss发布的地址
      "sslPort": 44362//端口号
    }//运行尽量采用自己的解决方案运行
  },
  "profiles": {
    "IIS Express": {
      "commandName": "IISExpress",
      "launchBrowser": true,
      "environmentVariables": {
        "ASPNETCORE_ENVIRONMENT": "Development"
      }
    },
    "CrossTheThreshold": {
      "commandName": "Project",
      "dotnetRunMessages": "true",
      "launchBrowser": true,//运行是否打开浏览器
      "applicationUrl": "https://localhost:5001;http://localhost:5000",
      "environmentVariables": {
        "ASPNETCORE_ENVIRONMENT": "Development" //当前运行的坏境,对应到appsettings.json有个Development的开发环境配置
      }
    }
  }
}

Program.cs:

程序的入口

  public class Program
    {
        public static void Main(string[] args)
        {
            CreateHostBuilder(args).Build().Run();
            //CreateHostBuilder:创建主机
            //Build():构建
            //Run():运行
        }

        public static IHostBuilder CreateHostBuilder(string[] args) =>
            Host.CreateDefaultBuilder(args)
                .ConfigureWebHostDefaults(webBuilder =>
                {
                    webBuilder.UseStartup<Startup>();
                });
    }
Startup.cs:

管道

 public class Startup
    {
        // 此方法由运行时调用。使用这种方法将服务添加到容器中。
        // 关于如何配置您的应用程序的更多信息,访问 https://go.microsoft.com/fwlink/?LinkID=398940
        public void ConfigureServices(IServiceCollection services)
        {
            //ConfigureServices:配置服务
        }

        // 此方法由运行时调用。使用这个方法来配置HTTP请求管道。
        public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
        {
            //Configure:配置管道请求
            if (env.IsDevelopment())
            {
                app.UseDeveloperExceptionPage();
            }

            app.UseRouting();
            
		    //配置一个默认的路由规则
            app.UseEndpoints(endpoints =>
            {//短路
                endpoints.MapGet("/", async context =>
                {
                    await context.Response.WriteAsync("Hello World!");
                });
            });
        }
    }
ConfigureServices:

配置服务

			//ConfigureServices:配置服务
            添加一个服务
            //services.AddControllers();
            配置视图
            //services.AddRazorPages();
            //services.AddMvc() //加载整个mvc框架所有功能
            //services.AddMvcCore() //加载mvc框架的核心功能,轻量级
            //services.AddControllersWithViews() //仅加载controller控制器和view视图
Configure:

配置管道请求

 //配置一个默认的路由规则
            app.UseEndpoints(endpoints =>
            {
                //短路
                endpoints.MapDefaultControllerRoute();//规则为/控制器/方法名/id参数
            });

_ViewImports.cshtml:

引用文件配置,就不用做相关的@using引用

@using CrossTheThreshold.Models;

_Layout.cshtml:

配置相关的样式放在Shared文件下

_ViewStart.cshtml:

呈现View文件的时候的启动文件

Action操作

[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-kVvnSrsO-1686218114535)(file:///C:\Users\29310\Documents\Tencent Files\2931044350\Image\C2C\TKVN{D20]1T2FTWF7Q5]{7Y.png)]

public ViewResult Index()
        {
            return View();
        }

        //跳转动作
        public RedirectResult Index2()
        {
            return Redirect("/Studet/Index");
        }

        //重新定向
        public RedirectToActionResult Index3()
        {
            //重构nameof(Index)
            return RedirectToAction(nameof(Index));
            // return RedirectToAction("Index", "Home");
        }

        //json格式转化
        public JsonResult Index4()
        {
            List<Studet> vm = new List<Studet>()
            {
                new Studet(1, "张三"),
                new Studet(2, "李四"),
                new Studet(3, "王五")
            };
            return Json(vm);
        }

        //list
        public List<Studet> Index5()
        {
            List<Studet> vm = new List<Studet>()
            {
                new Studet(1, "张三"),
                new Studet(2, "李四"),
                new Studet(3, "王五")
            };
            return vm;
        }

        //返回对象
        public OkObjectResult Index6()
        {
            List<Studet> vm = new List<Studet>()
            {
                new Studet(1, "张三"),
                new Studet(2, "李四"),
                new Studet(3, "王五")
            };
            return Ok(vm);
        }

        //返回string类型
        public ContentResult Index7()
        {
            return Content("查询成功");
        }

        //返回新的控制器
        // public RedirectToRouteResult Index8()
        // {
        //     return RedirectToRoute()
        // }
        //下载文件
        public FileResult Index9()
        {
            // MemoryStream类封装一个字节数组,在构造实例时可以使用一个字节数组作为参数,但是数组的长度无法调整。使用默认无参数构造函数创建实例,
            // 可以使用Write方法写入,随着字节数据的写入,数组的大小自动调整。
            // 在对MemoryStream类中数据流进行读取时,可以使用seek方法定位读取器的当前的位置,可以通过指定长度的数组一次性读取指定长度的数据。
            // ReadByte方法每次读取一个字节,并将字节返回一个整数值。
            // UnicodeEncoding类中定义了Unicode中UTF-16编码的相关功能。通过其中的方法将字符串转换为字节,也可以将字节转换为字符串。
            using MemoryStream ms = new();
            string textfile = "我是真爱粉";
            ms.Write(Encoding.UTF8.GetBytes(textfile)); //byte[]:Encoding.UTF8.GetBytes()
            return File(ms.ToArray(), "application/octet-stream", "demo.txt");
        }

视图使用

<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, user-scalable=no, initial-scale=1.0, maximum-scale=1.0, minimum-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <title>Document</title>
</head>
<body>

<div>我是index页面</div>

@{
    List<Studet> vm = new()
    {
        new Studet(1,"牛"),
        new Studet(2,"马"),
        new Studet(3,"蛇")
    };
    string sex = "男";
}
<table class="table table-hover " width="780px">
    <tr class="text-center">
        <th>序号</th>
        <th>名称</th>
    </tr>
    @foreach (var item in vm)
    {
        <tr class="text-center">
            <td>@item.id</td>
            <td>@item.name</td>
        </tr>
    }
</table>
@if (sex.Equals("男"))
{
    <h1>这里是男生能看到的东西</h1>
}
else
{
    //这个是编译成为纯文本
    @:这里的话只有女孩子看得到
}
</body>
</html>

mvc对象

View Data:

需要数据转换,用法在后台代码中ViewData[“自定义名字”]=传递值;调用在html里面用@VIewData[“自定义名字”]。

View Bag:

不需要数据转换,用法ViewBag.自定义名字= 传递值;调用在html里面用@ViewBag.自定义名字。

Temp Data:

不需要数据转换,用法在后台代码中TempData[“自定义名字”]=传递值;调用在html里面用@TempData[“自定义名字”]和ViewData不同的是Temp可以跨窗体传参,但是有生命周期只能被调用一次。

[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-a96gnmM8-1686218114536)(C:\Users\29310\AppData\Roaming\Typora\typora-user-images\image-20230321151111120.png)]
wData[“自定义名字”]=传递值;调用在html里面用@VIewData[“自定义名字”]。

View Bag:

不需要数据转换,用法ViewBag.自定义名字= 传递值;调用在html里面用@ViewBag.自定义名字。

Temp Data:

不需要数据转换,用法在后台代码中TempData[“自定义名字”]=传递值;调用在html里面用@TempData[“自定义名字”]和ViewData不同的是Temp可以跨窗体传参,但是有生命周期只能被调用一次。

[外链图片转存中…(img-a96gnmM8-1686218114536)]

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

IT中的好男人

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值