【小5聊】.net core 3.1 配置MVC路由和API

微软的core技术更新换代真够快的,从.net framework开始到core,还没来得及熟悉有更新。每次技术或框架的更新,避免不了类和方法的变动,不得不又重新调整框架代码,版本更新快淘汰也快,截至2022年11月3日,core2.1、core5.0都不再支持维护。

拥抱变化,才能紧跟技术前沿

1、启动类代码

using Microsoft.AspNetCore;
using Microsoft.AspNetCore.Hosting;

namespace Core31
{
    public class Program
    {
        public static void Main(string[] args)
        {
            CreateWebHostBuilder(args).Build().Run();
        }

        public static IWebHostBuilder CreateWebHostBuilder(string[] args) =>
            WebHost.CreateDefaultBuilder(args)
                .UseStartup<Startup>();
    }
}

 2、配置服务方法

添加MVC视图和API组件

//===配置MVC-视图===
services.AddControllersWithViews(options =>
{
    options.Filters.Add(typeof(ExceptionFilter)); //全局异常过滤
});

//===配置MVC-API===
services.AddControllers();

3、配置方法

启动MVC,特别要主要记得加上app.UserRouting()方法,否则可能还是会报错

// 配置MVC视图
app.UseRouting();
app.UseEndpoints(endpoints =>
{
    endpoints.MapControllerRoute(
        name: "default",
        pattern: "{controller=Test}/{action=Index}/{id?}");
});

//===配置MVC-API===
app.UseEndpoints(endpoints =>
{
    endpoints.MapControllers();
});

4、版本下载地址

5、版本支持说明

目前.NET Core 3.1算是比较稳定且在维护的 

6、遇到异常信息

Synchronous operations are disallowed. Call WriteAsync or set AllowSynchronousIO to true instead

// If using Kestrel:
services.Configure<KestrelServerOptions>(options =>
{
    options.AllowSynchronousIO = true;
});

// If using IIS:
services.Configure<IISServerOptions>(options =>
{
    options.AllowSynchronousIO = true;
});

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

全栈小5

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

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

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

打赏作者

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

抵扣说明:

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

余额充值