zoukankan      html  css  js  c++  java
  • .NetCore -MVC 路由的配置

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Threading.Tasks;
    using Microsoft.AspNetCore.Builder;
    using Microsoft.AspNetCore.Hosting;
    using Microsoft.AspNetCore.Http;
    using Microsoft.Extensions.DependencyInjection;
    //参数验证,路由的参数验证配置
    using Microsoft.AspNetCore.Routing;     
    using Microsoft.AspNetCore.Routing.Constraints;
    
    namespace Han.oi.Web
    {
        public class Startup
        {
            // This method gets called by the runtime. Use this method to add services to the container.
            // For more information on how to configure your application, visit https://go.microsoft.com/fwlink/?LinkID=398940
            public void ConfigureServices(IServiceCollection services)
            {
                //引入MVC模块
                services.AddMvc();
            }
    
            // This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
            public void Configure(IApplicationBuilder app, IHostingEnvironment env)
            {
                if (env.IsDevelopment())
                {
                    app.UseDeveloperExceptionPage();
                }
    
                app.UseMvc(routes =>
                {
                    routes.MapRoute(
                        name: "Default",
                        template: "{controller}/{action}",
                        defaults: new { controller = "Home", action = "index" }
                    );
                    routes.MapRoute(
                        name: "Han1",
                        template: "{controller}/{action}",
                        defaults: new { controller = "Han1", action = "Time"}
                    );
    
                   /*  routes.MapRoute(
                       name: "Tutorial",
                       template: "{controller}/{action}/{name}/{age?}",
                       defaults: new { controller = "Tutorial"}
                    ); */
    
                   /*  routes.MapRoute(
                        name: "Tutorial",
                        template: "{controller}/{action}/{age}/{name}",
                        defaults: new { controller = "Tutorial",action="Welcome",name = "韩"},
                        constraints: new { name = new MaxLengthRouteConstraint(5) }
                    ); */
    
                    routes.MapRoute(
                       name: "Tutorial_1",
                       template: "{controller}/{action}/{age:range(1,150)}/{name:maxlength(5)}",
                       defaults: new { controller = "Tutorial", action = "Welcome", name = "" }
                    );
    
                    /* routes.MapRoute(
                       name: "Tutorial_1",
                       template: "hello/{action}/{age:range(1,150)}/{name:maxlength(5)}",
                       defaults: new { controller = "Tutorial", action = "Welcome", name = "韩" }
                    ); */
    
                    routes.MapRoute(
                       name: "jiaocheng_1",
                       template: "jioachen/{action}.html"
                    );
                });
    
    
            }
        }
    }
    引入包的名称在项目根目录输入(连接Mysql的数据包)
    
    dotnet add package Pomelo.EntityFrameworkCore.MySql --version 2.1.4

    这个版本引入用不了,换个版本
    dotnet add package Pomelo.EntityFrameworkCore.MySql --version 2.0.1
    可以用

      

     
    1.创建解决方案
        dotnet new sln -n Ken.Tutorial
    2.创建.NetCore项目
        dotnet new web -n Ken.Tutorial.Web
    3.将项目移到解决方案中
       dotnet sln add Ken.Tutorial.Web 
    C#中的jar包配置
    
    <Project Sdk="Microsoft.NET.Sdk.Web">
      <PropertyGroup>
        <TargetFramework>netcoreapp2.1</TargetFramework>
      </PropertyGroup>
      <ItemGroup>
        <Folder Include="wwwroot"/>
      </ItemGroup>
      <ItemGroup>
        <PackageReference Include="Microsoft.AspNetCore.App"/>
        <PackageReference Include="Microsoft.AspNetCore.Razor.Design" Version="2.1.2" PrivateAssets="All"/>
        <PackageReference Include="Pomelo.EntityFrameworkCore.MySql" Version="2.1.1"/>
      </ItemGroup>
    </Project>
    
    

    EF Core + Linq to entity 访问数据库

    EF Core + 原生SQL 访问数据库

    https://www.cnblogs.com/ken-io/p/aspnet-core-tutorial-entity-framework-core-mysql.html
  • 相关阅读:
    【算法】LeetCode算法题-Count And Say
    【算法】LeetCode算法题-Search Insert Position
    使用POI设置excel背景色
    Ubuntu中开启MySQL远程访问功能,并将另一个数据库服务器中的数据迁移到新的服务器中
    利用mybatis_generator自动生成Dao、Model、Mapping相关文件
    Meven笔记
    js调用百度地图API创建地图
    MySQL中日期与字符串相互转换,并进行日期比较查询
    java中将汉字转换成16进制
    Java中将16进制字符串转换成汉字
  • 原文地址:https://www.cnblogs.com/han-guang-xue/p/10559265.html
Copyright © 2011-2022 走看看